blob: 78723442246c18a139624911b4380a2a201cd99a [file] [log] [blame]
Ethan Yonker74db1572015-10-28 12:44:49 -05001/*
bigbiffdf8436b2020-08-30 16:22:34 -04002 Copyright 2012 - 2020 TeamWin
Ethan Yonker74db1572015-10-28 12:44:49 -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// 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>
Dees_Troy51a0e822012-09-05 15:24:24 -040026#include <time.h>
27#include <unistd.h>
thata9dd9f02017-02-23 23:08:56 +010028#include <pthread.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040029
30#include <string>
31
32extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000033#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040034}
bigbiffd81833a2021-01-17 11:06:57 -050035#include "minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#include "rapidxml.hpp"
38#include "objects.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050039#include "gui.hpp"
40#include "twmsg.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041
Ethan Yonker74db1572015-10-28 12:44:49 -050042#define GUI_CONSOLE_BUFFER_SIZE 512
Dees_Troy51a0e822012-09-05 15:24:24 -040043
thata9dd9f02017-02-23 23:08:56 +010044static pthread_mutex_t console_lock;
45static size_t last_message_count = 0;
46static std::vector<Message> gMessages;
Ethan Yonker74db1572015-10-28 12:44:49 -050047
thata9dd9f02017-02-23 23:08:56 +010048static std::vector<std::string> gConsole;
49static std::vector<std::string> gConsoleColor;
50static FILE* ors_file = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -040051
thata9dd9f02017-02-23 23:08:56 +010052struct InitMutex
Dees_Troy51a0e822012-09-05 15:24:24 -040053{
thata9dd9f02017-02-23 23:08:56 +010054 InitMutex() { pthread_mutex_init(&console_lock, NULL); }
55} initMutex;
56
57static void internal_gui_print(const char *color, char *buf)
58{
59 // make sure to flush any outstanding messages first to preserve order of outputs
60 GUIConsole::Translate_Now();
61
62 fputs(buf, stdout);
63 if (ors_file) {
64 fprintf(ors_file, "%s", buf);
65 fflush(ors_file);
66 }
67
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020068 char *start, *next;
Dees_Troy51a0e822012-09-05 15:24:24 -040069
70 if (buf[0] == '\n' && strlen(buf) < 2) {
71 // This prevents the double lines bug seen in the console during zip installs
72 return;
73 }
74
thata9dd9f02017-02-23 23:08:56 +010075 pthread_mutex_lock(&console_lock);
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010076 for (start = next = buf; *next != '\0';)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020077 {
78 if (*next == '\n')
79 {
80 *next = '\0';
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010081 gConsole.push_back(start);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050082 gConsoleColor.push_back(color);
Dees_Troy51a0e822012-09-05 15:24:24 -040083
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010084 start = ++next;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 }
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010086 else
87 ++next;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 }
Dees_Troy51a0e822012-09-05 15:24:24 -040089
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010090 // The text after last \n (or whole string if there is no \n)
Matt Mowera8a89d12016-12-30 18:10:37 -060091 if (*start) {
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010092 gConsole.push_back(start);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050093 gConsoleColor.push_back(color);
94 }
thata9dd9f02017-02-23 23:08:56 +010095 pthread_mutex_unlock(&console_lock);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050096}
97
98extern "C" void gui_print(const char *fmt, ...)
99{
Ethan Yonker74db1572015-10-28 12:44:49 -0500100 char buf[GUI_CONSOLE_BUFFER_SIZE]; // We're going to limit a single request to 512 bytes
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500101
102 va_list ap;
103 va_start(ap, fmt);
Ethan Yonker74db1572015-10-28 12:44:49 -0500104 vsnprintf(buf, GUI_CONSOLE_BUFFER_SIZE, fmt, ap);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500105 va_end(ap);
106
thata9dd9f02017-02-23 23:08:56 +0100107 internal_gui_print("normal", buf);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500108}
109
110extern "C" void gui_print_color(const char *color, const char *fmt, ...)
111{
Ethan Yonker74db1572015-10-28 12:44:49 -0500112 char buf[GUI_CONSOLE_BUFFER_SIZE]; // We're going to limit a single request to 512 bytes
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500113
114 va_list ap;
115 va_start(ap, fmt);
Ethan Yonker74db1572015-10-28 12:44:49 -0500116 vsnprintf(buf, GUI_CONSOLE_BUFFER_SIZE, fmt, ap);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500117 va_end(ap);
118
thata9dd9f02017-02-23 23:08:56 +0100119 internal_gui_print(color, buf);
Dees_Troy51a0e822012-09-05 15:24:24 -0400120}
121
Ethan Yonker03a42f62014-08-08 11:03:51 -0500122extern "C" void gui_set_FILE(FILE* f)
123{
124 ors_file = f;
125}
126
Ethan Yonker74db1572015-10-28 12:44:49 -0500127void gui_msg(const char* text)
128{
129 if (text) {
130 Message msg = Msg(text);
131 gui_msg(msg);
132 }
133}
134
135void gui_warn(const char* text)
136{
137 if (text) {
138 Message msg = Msg(msg::kWarning, text);
139 gui_msg(msg);
140 }
141}
142
143void gui_err(const char* text)
144{
145 if (text) {
146 Message msg = Msg(msg::kError, text);
147 gui_msg(msg);
148 }
149}
150
151void gui_highlight(const char* text)
152{
153 if (text) {
154 Message msg = Msg(msg::kHighlight, text);
155 gui_msg(msg);
156 }
157}
158
159void gui_msg(Message msg)
160{
161 std::string output = msg;
162 output += "\n";
163 fputs(output.c_str(), stdout);
164 if (ors_file) {
165 fprintf(ors_file, "%s", output.c_str());
166 fflush(ors_file);
167 }
thata9dd9f02017-02-23 23:08:56 +0100168 pthread_mutex_lock(&console_lock);
Ethan Yonker74db1572015-10-28 12:44:49 -0500169 gMessages.push_back(msg);
thata9dd9f02017-02-23 23:08:56 +0100170 pthread_mutex_unlock(&console_lock);
Ethan Yonker74db1572015-10-28 12:44:49 -0500171}
172
thata9dd9f02017-02-23 23:08:56 +0100173void GUIConsole::Translate_Now()
174{
175 pthread_mutex_lock(&console_lock);
Ethan Yonker74db1572015-10-28 12:44:49 -0500176 size_t message_count = gMessages.size();
177 if (message_count <= last_message_count)
thata9dd9f02017-02-23 23:08:56 +0100178 {
179 pthread_mutex_unlock(&console_lock);
Ethan Yonker74db1572015-10-28 12:44:49 -0500180 return;
thata9dd9f02017-02-23 23:08:56 +0100181 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500182
183 for (size_t m = last_message_count; m < message_count; m++) {
184 std::string message = gMessages[m];
185 std::string color = "normal";
186 if (gMessages[m].GetKind() == msg::kError)
187 color = "error";
188 else if (gMessages[m].GetKind() == msg::kHighlight)
189 color = "highlight";
190 else if (gMessages[m].GetKind() == msg::kWarning)
191 color = "warning";
192 gConsole.push_back(message);
193 gConsoleColor.push_back(color);
194 }
195 last_message_count = message_count;
thata9dd9f02017-02-23 23:08:56 +0100196 pthread_mutex_unlock(&console_lock);
197}
198
199void GUIConsole::Clear_For_Retranslation()
200{
201 pthread_mutex_lock(&console_lock);
202 last_message_count = 0;
203 gConsole.clear();
204 gConsoleColor.clear();
205 pthread_mutex_unlock(&console_lock);
Ethan Yonker74db1572015-10-28 12:44:49 -0500206}
207
that8d46c092015-02-26 01:30:04 +0100208GUIConsole::GUIConsole(xml_node<>* node) : GUIScrollList(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400209{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200210 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -0400211
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 mLastCount = 0;
that8d46c092015-02-26 01:30:04 +0100213 scrollToEnd = true;
214 mSlideoutX = mSlideoutY = mSlideoutW = mSlideoutH = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200215 mSlideout = 0;
that8d46c092015-02-26 01:30:04 +0100216 mSlideoutState = visible;
Dees_Troy51a0e822012-09-05 15:24:24 -0400217
that8d46c092015-02-26 01:30:04 +0100218 allowSelection = false; // console doesn't support list item selections
Dees_Troy51a0e822012-09-05 15:24:24 -0400219
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200220 if (!node)
221 {
Matt Mowera8a89d12016-12-30 18:10:37 -0600222 mRenderX = 0;
223 mRenderY = 0;
224 mRenderW = gr_fb_width();
225 mRenderH = gr_fb_height();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200226 }
227 else
228 {
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600229 child = FindNode(node, "color");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200230 if (child)
231 {
that8d46c092015-02-26 01:30:04 +0100232 mFontColor = LoadAttrColor(child, "foreground", mFontColor);
thatf6ed8fc2015-02-14 20:23:16 +0100233 mBackgroundColor = LoadAttrColor(child, "background", mBackgroundColor);
that8d46c092015-02-26 01:30:04 +0100234 //mScrollColor = LoadAttrColor(child, "scroll", mScrollColor);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400236
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600237 child = FindNode(node, "slideout");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200238 if (child)
239 {
240 mSlideout = 1;
that8d46c092015-02-26 01:30:04 +0100241 mSlideoutState = hidden;
Ethan Yonker591b9202015-03-11 11:17:15 -0500242 LoadPlacement(child, &mSlideoutX, &mSlideoutY, &mSlideoutW, &mSlideoutH, &mPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -0400243
thatf6ed8fc2015-02-14 20:23:16 +0100244 mSlideoutImage = LoadAttrImage(child, "resource");
Dees_Troy51a0e822012-09-05 15:24:24 -0400245
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 if (mSlideoutImage && mSlideoutImage->GetResource())
247 {
thatf6ed8fc2015-02-14 20:23:16 +0100248 mSlideoutW = mSlideoutImage->GetWidth();
249 mSlideoutH = mSlideoutImage->GetHeight();
Ethan Yonker591b9202015-03-11 11:17:15 -0500250 if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY) {
251 mSlideoutX = mSlideoutX - (mSlideoutW / 2);
252 if (mPlacement == CENTER) {
253 mSlideoutY = mSlideoutY - (mSlideoutH / 2);
254 }
255 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 }
257 }
258 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400259}
260
261int GUIConsole::RenderSlideout(void)
262{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200263 if (!mSlideoutImage || !mSlideoutImage->GetResource())
264 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400265
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200266 gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY);
267 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400268}
269
that8d46c092015-02-26 01:30:04 +0100270int GUIConsole::RenderConsole(void)
271{
Ethan Yonker74db1572015-10-28 12:44:49 -0500272 Translate_Now();
thata9dd9f02017-02-23 23:08:56 +0100273 pthread_mutex_lock(&console_lock);
Ethan Yonker44925ad2015-07-22 12:33:59 -0500274 AddLines(&gConsole, &gConsoleColor, &mLastCount, &rConsole, &rConsoleColor);
thata9dd9f02017-02-23 23:08:56 +0100275 pthread_mutex_unlock(&console_lock);
that8d46c092015-02-26 01:30:04 +0100276 GUIScrollList::Render();
Dees_Troy51a0e822012-09-05 15:24:24 -0400277
that8d46c092015-02-26 01:30:04 +0100278 // if last line is fully visible, keep tracking the last line when new lines are added
279 int bottom_offset = GetDisplayRemainder() - actualItemHeight;
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500280 bool isAtBottom = firstDisplayedItem == (int)GetItemCount() - GetDisplayItemCount() - (bottom_offset != 0) && y_offset == bottom_offset;
that8d46c092015-02-26 01:30:04 +0100281 if (isAtBottom)
282 scrollToEnd = true;
283#if 0
284 // debug - show if we are tracking the last line
285 if (scrollToEnd) {
286 gr_color(0,255,0,255);
287 gr_fill(mRenderX+mRenderW-5, mRenderY+mRenderH-5, 5, 5);
288 } else {
289 gr_color(255,0,0,255);
290 gr_fill(mRenderX+mRenderW-5, mRenderY+mRenderH-5, 5, 5);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200291 }
that8d46c092015-02-26 01:30:04 +0100292#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200293 return (mSlideout ? RenderSlideout() : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400294}
295
296int GUIConsole::Render(void)
297{
Matt Mowera8a89d12016-12-30 18:10:37 -0600298 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100299 return 0;
300
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200301 if (mSlideout && mSlideoutState == hidden)
302 return RenderSlideout();
303
304 return RenderConsole();
Dees_Troy51a0e822012-09-05 15:24:24 -0400305}
306
307int GUIConsole::Update(void)
308{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200309 if (mSlideout && mSlideoutState != visible)
310 {
311 if (mSlideoutState == hidden)
312 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400313
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200314 if (mSlideoutState == request_hide)
315 mSlideoutState = hidden;
Dees_Troy51a0e822012-09-05 15:24:24 -0400316
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200317 if (mSlideoutState == request_show)
318 mSlideoutState = visible;
Dees_Troy51a0e822012-09-05 15:24:24 -0400319
that8d46c092015-02-26 01:30:04 +0100320 // Any time we activate the console, we reset the position
321 SetVisibleListLocation(rConsole.size() - 1);
322 mUpdate = 1;
323 scrollToEnd = true;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200324 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400325
thata9dd9f02017-02-23 23:08:56 +0100326 pthread_mutex_lock(&console_lock);
327 bool addedNewText = AddLines(&gConsole, &gConsoleColor, &mLastCount, &rConsole, &rConsoleColor);
328 pthread_mutex_unlock(&console_lock);
329 if (addedNewText) {
that8d46c092015-02-26 01:30:04 +0100330 // someone added new text
331 // at least the scrollbar must be updated, even if the new lines are currently not visible
332 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200333 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400334
that8d46c092015-02-26 01:30:04 +0100335 if (scrollToEnd) {
336 // keep the last line in view
337 SetVisibleListLocation(rConsole.size() - 1);
338 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400339
that8d46c092015-02-26 01:30:04 +0100340 GUIScrollList::Update();
341
342 if (mUpdate) {
343 mUpdate = 0;
344 if (Render() == 0)
345 return 2;
346 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200347 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400348}
349
350// IsInRegion - Checks if the request is handled by this object
thatf8194e22015-01-29 01:05:01 +0100351// Return 1 if this object handles the request, 0 if not
Dees_Troy51a0e822012-09-05 15:24:24 -0400352int GUIConsole::IsInRegion(int x, int y)
353{
that8d46c092015-02-26 01:30:04 +0100354 if (mSlideout) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200355 // Check if they tapped the slideout button
that8d46c092015-02-26 01:30:04 +0100356 if (x >= mSlideoutX && x < mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200357 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400358
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200359 // If we're only rendering the slideout, bail now
360 if (mSlideoutState == hidden)
361 return 0;
362 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400363
that8d46c092015-02-26 01:30:04 +0100364 return GUIScrollList::IsInRegion(x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400365}
366
367// NotifyTouch - Notify of a touch event
368// Return 0 on success, >0 to ignore remainder of touch, and <0 on error
369int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y)
370{
Matt Mowera8a89d12016-12-30 18:10:37 -0600371 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100372 return -1;
373
that8d46c092015-02-26 01:30:04 +0100374 if (mSlideout && x >= mSlideoutX && x < mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH) {
375 if (state == TOUCH_START) {
376 if (mSlideoutState == hidden)
377 mSlideoutState = request_show;
378 else if (mSlideoutState == visible)
379 mSlideoutState = request_hide;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200380 }
that8d46c092015-02-26 01:30:04 +0100381 return 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200382 }
that8d46c092015-02-26 01:30:04 +0100383 scrollToEnd = false;
384 return GUIScrollList::NotifyTouch(state, x, y);
385}
386
387size_t GUIConsole::GetItemCount()
388{
389 return rConsole.size();
390}
391
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500392void GUIConsole::RenderItem(size_t itemindex, int yPos, bool selected __unused)
that8d46c092015-02-26 01:30:04 +0100393{
394 // Set the color for the font
395 if (rConsoleColor[itemindex] == "normal") {
396 gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha);
397 } else {
398 COLOR FontColor;
399 std::string color = rConsoleColor[itemindex];
400 ConvertStrToColor(color, &FontColor);
401 FontColor.alpha = 255;
402 gr_color(FontColor.red, FontColor.green, FontColor.blue, FontColor.alpha);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200403 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400404
that8d46c092015-02-26 01:30:04 +0100405 // render text
406 const char* text = rConsole[itemindex].c_str();
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500407 gr_textEx_scaleW(mRenderX, yPos, text, mFont->GetResource(), mRenderW, TOP_LEFT, 0);
that8d46c092015-02-26 01:30:04 +0100408}
Dees_Troy51a0e822012-09-05 15:24:24 -0400409
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500410void GUIConsole::NotifySelect(size_t item_selected __unused)
that8d46c092015-02-26 01:30:04 +0100411{
412 // do nothing - console ignores selections
Dees_Troy51a0e822012-09-05 15:24:24 -0400413}