blob: b5204fb7c7e4a2014f320450485f60c1743cad8d [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#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041}
42
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)
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050081 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 {
that8d46c092015-02-26 01:30:04 +0100208 mRenderX = 0; mRenderY = 0; mRenderW = gr_fb_width(); mRenderH = gr_fb_height();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200209 }
210 else
211 {
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600212 child = FindNode(node, "color");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 if (child)
214 {
that8d46c092015-02-26 01:30:04 +0100215 mFontColor = LoadAttrColor(child, "foreground", mFontColor);
thatf6ed8fc2015-02-14 20:23:16 +0100216 mBackgroundColor = LoadAttrColor(child, "background", mBackgroundColor);
that8d46c092015-02-26 01:30:04 +0100217 //mScrollColor = LoadAttrColor(child, "scroll", mScrollColor);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200218 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400219
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600220 child = FindNode(node, "slideout");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 if (child)
222 {
223 mSlideout = 1;
that8d46c092015-02-26 01:30:04 +0100224 mSlideoutState = hidden;
Ethan Yonker591b9202015-03-11 11:17:15 -0500225 LoadPlacement(child, &mSlideoutX, &mSlideoutY, &mSlideoutW, &mSlideoutH, &mPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -0400226
thatf6ed8fc2015-02-14 20:23:16 +0100227 mSlideoutImage = LoadAttrImage(child, "resource");
Dees_Troy51a0e822012-09-05 15:24:24 -0400228
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200229 if (mSlideoutImage && mSlideoutImage->GetResource())
230 {
thatf6ed8fc2015-02-14 20:23:16 +0100231 mSlideoutW = mSlideoutImage->GetWidth();
232 mSlideoutH = mSlideoutImage->GetHeight();
Ethan Yonker591b9202015-03-11 11:17:15 -0500233 if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY) {
234 mSlideoutX = mSlideoutX - (mSlideoutW / 2);
235 if (mPlacement == CENTER) {
236 mSlideoutY = mSlideoutY - (mSlideoutH / 2);
237 }
238 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 }
240 }
241 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400242}
243
244int GUIConsole::RenderSlideout(void)
245{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 if (!mSlideoutImage || !mSlideoutImage->GetResource())
247 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400248
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY);
250 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251}
252
that8d46c092015-02-26 01:30:04 +0100253int GUIConsole::RenderConsole(void)
254{
Ethan Yonker74db1572015-10-28 12:44:49 -0500255 Translate_Now();
Ethan Yonker44925ad2015-07-22 12:33:59 -0500256 AddLines(&gConsole, &gConsoleColor, &mLastCount, &rConsole, &rConsoleColor);
that8d46c092015-02-26 01:30:04 +0100257 GUIScrollList::Render();
Dees_Troy51a0e822012-09-05 15:24:24 -0400258
that8d46c092015-02-26 01:30:04 +0100259 // if last line is fully visible, keep tracking the last line when new lines are added
260 int bottom_offset = GetDisplayRemainder() - actualItemHeight;
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500261 bool isAtBottom = firstDisplayedItem == (int)GetItemCount() - GetDisplayItemCount() - (bottom_offset != 0) && y_offset == bottom_offset;
that8d46c092015-02-26 01:30:04 +0100262 if (isAtBottom)
263 scrollToEnd = true;
264#if 0
265 // debug - show if we are tracking the last line
266 if (scrollToEnd) {
267 gr_color(0,255,0,255);
268 gr_fill(mRenderX+mRenderW-5, mRenderY+mRenderH-5, 5, 5);
269 } else {
270 gr_color(255,0,0,255);
271 gr_fill(mRenderX+mRenderW-5, mRenderY+mRenderH-5, 5, 5);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200272 }
that8d46c092015-02-26 01:30:04 +0100273#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200274 return (mSlideout ? RenderSlideout() : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400275}
276
277int GUIConsole::Render(void)
278{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100279 if(!isConditionTrue())
280 return 0;
281
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200282 if (mSlideout && mSlideoutState == hidden)
283 return RenderSlideout();
284
285 return RenderConsole();
Dees_Troy51a0e822012-09-05 15:24:24 -0400286}
287
288int GUIConsole::Update(void)
289{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200290 if (mSlideout && mSlideoutState != visible)
291 {
292 if (mSlideoutState == hidden)
293 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400294
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 if (mSlideoutState == request_hide)
296 mSlideoutState = hidden;
Dees_Troy51a0e822012-09-05 15:24:24 -0400297
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200298 if (mSlideoutState == request_show)
299 mSlideoutState = visible;
Dees_Troy51a0e822012-09-05 15:24:24 -0400300
that8d46c092015-02-26 01:30:04 +0100301 // Any time we activate the console, we reset the position
302 SetVisibleListLocation(rConsole.size() - 1);
303 mUpdate = 1;
304 scrollToEnd = true;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200305 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400306
Ethan Yonker44925ad2015-07-22 12:33:59 -0500307 if (AddLines(&gConsole, &gConsoleColor, &mLastCount, &rConsole, &rConsoleColor)) {
that8d46c092015-02-26 01:30:04 +0100308 // someone added new text
309 // at least the scrollbar must be updated, even if the new lines are currently not visible
310 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200311 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400312
that8d46c092015-02-26 01:30:04 +0100313 if (scrollToEnd) {
314 // keep the last line in view
315 SetVisibleListLocation(rConsole.size() - 1);
316 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400317
that8d46c092015-02-26 01:30:04 +0100318 GUIScrollList::Update();
319
320 if (mUpdate) {
321 mUpdate = 0;
322 if (Render() == 0)
323 return 2;
324 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200325 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400326}
327
328// IsInRegion - Checks if the request is handled by this object
thatf8194e22015-01-29 01:05:01 +0100329// Return 1 if this object handles the request, 0 if not
Dees_Troy51a0e822012-09-05 15:24:24 -0400330int GUIConsole::IsInRegion(int x, int y)
331{
that8d46c092015-02-26 01:30:04 +0100332 if (mSlideout) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200333 // Check if they tapped the slideout button
that8d46c092015-02-26 01:30:04 +0100334 if (x >= mSlideoutX && x < mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200335 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400336
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200337 // If we're only rendering the slideout, bail now
338 if (mSlideoutState == hidden)
339 return 0;
340 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400341
that8d46c092015-02-26 01:30:04 +0100342 return GUIScrollList::IsInRegion(x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400343}
344
345// NotifyTouch - Notify of a touch event
346// Return 0 on success, >0 to ignore remainder of touch, and <0 on error
347int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y)
348{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100349 if(!isConditionTrue())
350 return -1;
351
that8d46c092015-02-26 01:30:04 +0100352 if (mSlideout && x >= mSlideoutX && x < mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH) {
353 if (state == TOUCH_START) {
354 if (mSlideoutState == hidden)
355 mSlideoutState = request_show;
356 else if (mSlideoutState == visible)
357 mSlideoutState = request_hide;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200358 }
that8d46c092015-02-26 01:30:04 +0100359 return 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200360 }
that8d46c092015-02-26 01:30:04 +0100361 scrollToEnd = false;
362 return GUIScrollList::NotifyTouch(state, x, y);
363}
364
365size_t GUIConsole::GetItemCount()
366{
367 return rConsole.size();
368}
369
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500370void GUIConsole::RenderItem(size_t itemindex, int yPos, bool selected __unused)
that8d46c092015-02-26 01:30:04 +0100371{
372 // Set the color for the font
373 if (rConsoleColor[itemindex] == "normal") {
374 gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha);
375 } else {
376 COLOR FontColor;
377 std::string color = rConsoleColor[itemindex];
378 ConvertStrToColor(color, &FontColor);
379 FontColor.alpha = 255;
380 gr_color(FontColor.red, FontColor.green, FontColor.blue, FontColor.alpha);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200381 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400382
that8d46c092015-02-26 01:30:04 +0100383 // render text
384 const char* text = rConsole[itemindex].c_str();
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500385 gr_textEx_scaleW(mRenderX, yPos, text, mFont->GetResource(), mRenderW, TOP_LEFT, 0);
that8d46c092015-02-26 01:30:04 +0100386}
Dees_Troy51a0e822012-09-05 15:24:24 -0400387
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500388void GUIConsole::NotifySelect(size_t item_selected __unused)
that8d46c092015-02-26 01:30:04 +0100389{
390 // do nothing - console ignores selections
Dees_Troy51a0e822012-09-05 15:24:24 -0400391}