blob: 3623f5558335bbdfc4fa7bd21e485515e6e4bf55 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// console.cpp - GUIConsole object
2
3#include <stdarg.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <fcntl.h>
8#include <sys/reboot.h>
9#include <sys/stat.h>
10#include <sys/time.h>
11#include <sys/mman.h>
12#include <sys/types.h>
13#include <sys/ioctl.h>
14#include <time.h>
15#include <unistd.h>
16#include <stdlib.h>
17
18#include <string>
19
20extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000021#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040022#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040023}
24
25#include "rapidxml.hpp"
26#include "objects.hpp"
27
28
29static std::vector<std::string> gConsole;
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050030static std::vector<std::string> gConsoleColor;
Ethan Yonker03a42f62014-08-08 11:03:51 -050031static FILE* ors_file;
Dees_Troy51a0e822012-09-05 15:24:24 -040032
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050033extern "C" void __gui_print(const char *color, char *buf)
Dees_Troy51a0e822012-09-05 15:24:24 -040034{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020035 char *start, *next;
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37 if (buf[0] == '\n' && strlen(buf) < 2) {
38 // This prevents the double lines bug seen in the console during zip installs
39 return;
40 }
41
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010042 for (start = next = buf; *next != '\0';)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020043 {
44 if (*next == '\n')
45 {
46 *next = '\0';
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010047 gConsole.push_back(start);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050048 gConsoleColor.push_back(color);
Dees_Troy51a0e822012-09-05 15:24:24 -040049
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010050 start = ++next;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020051 }
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010052 else
53 ++next;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020054 }
Dees_Troy51a0e822012-09-05 15:24:24 -040055
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010056 // The text after last \n (or whole string if there is no \n)
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050057 if(*start) {
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010058 gConsole.push_back(start);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050059 gConsoleColor.push_back(color);
60 }
Ethan Yonker03a42f62014-08-08 11:03:51 -050061 if (ors_file) {
62 fprintf(ors_file, "%s\n", buf);
63 fflush(ors_file);
64 }
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050065}
66
67extern "C" void gui_print(const char *fmt, ...)
68{
69 char buf[512]; // We're going to limit a single request to 512 bytes
70
71 va_list ap;
72 va_start(ap, fmt);
73 vsnprintf(buf, 512, fmt, ap);
74 va_end(ap);
75
76 fputs(buf, stdout);
77
78 __gui_print("normal", buf);
79 return;
80}
81
82extern "C" void gui_print_color(const char *color, const char *fmt, ...)
83{
84 char buf[512]; // We're going to limit a single request to 512 bytes
85
86 va_list ap;
87 va_start(ap, fmt);
88 vsnprintf(buf, 512, fmt, ap);
89 va_end(ap);
90
91 fputs(buf, stdout);
92
93 __gui_print(color, buf);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020094 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040095}
96
Ethan Yonker03a42f62014-08-08 11:03:51 -050097extern "C" void gui_set_FILE(FILE* f)
98{
99 ors_file = f;
100}
101
Vojtech Bocekede51c52014-02-07 23:58:09 +0100102GUIConsole::GUIConsole(xml_node<>* node) : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400103{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200104 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -0400105
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200106 mFont = NULL;
107 mCurrentLine = -1;
108 memset(&mForegroundColor, 255, sizeof(COLOR));
109 memset(&mBackgroundColor, 0, sizeof(COLOR));
110 mBackgroundColor.alpha = 255;
111 memset(&mScrollColor, 0x08, sizeof(COLOR));
112 mScrollColor.alpha = 255;
113 mLastCount = 0;
114 mSlideout = 0;
Dees Troy31218ec2014-02-25 20:35:56 +0000115 RenderCount = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200116 mSlideoutState = hidden;
Dees Troy31218ec2014-02-25 20:35:56 +0000117 mRender = true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400118
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200119 mRenderX = 0; mRenderY = 0; mRenderW = gr_fb_width(); mRenderH = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400120
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200121 if (!node)
122 {
123 mSlideoutX = 0; mSlideoutY = 0; mSlideoutW = 0; mSlideoutH = 0;
124 mConsoleX = 0; mConsoleY = 0; mConsoleW = gr_fb_width(); mConsoleH = gr_fb_height();
125 }
126 else
127 {
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600128 mFont = LoadAttrFont(FindNode(node, "font"), "resource");
Dees_Troy51a0e822012-09-05 15:24:24 -0400129
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600130 child = FindNode(node, "color");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200131 if (child)
132 {
thatf6ed8fc2015-02-14 20:23:16 +0100133 mForegroundColor = LoadAttrColor(child, "foreground", mForegroundColor);
134 mBackgroundColor = LoadAttrColor(child, "background", mBackgroundColor);
135 mScrollColor = LoadAttrColor(child, "scroll", mScrollColor);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400137
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600139 LoadPlacement(FindNode(node, "placement"), &mConsoleX, &mConsoleY, &mConsoleW, &mConsoleH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400140
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600141 child = FindNode(node, "slideout");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200142 if (child)
143 {
144 mSlideout = 1;
145 LoadPlacement(child, &mSlideoutX, &mSlideoutY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400146
thatf6ed8fc2015-02-14 20:23:16 +0100147 mSlideoutImage = LoadAttrImage(child, "resource");
Dees_Troy51a0e822012-09-05 15:24:24 -0400148
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200149 if (mSlideoutImage && mSlideoutImage->GetResource())
150 {
thatf6ed8fc2015-02-14 20:23:16 +0100151 mSlideoutW = mSlideoutImage->GetWidth();
152 mSlideoutH = mSlideoutImage->GetHeight();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 }
154 }
155 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400156
thatf6ed8fc2015-02-14 20:23:16 +0100157 mFontHeight = mFont->GetHeight();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
159 SetRenderPos(mConsoleX, mConsoleY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400160}
161
162int GUIConsole::RenderSlideout(void)
163{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200164 if (!mSlideoutImage || !mSlideoutImage->GetResource())
165 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400166
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200167 gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY);
168 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400169}
170
171int GUIConsole::RenderConsole(void)
172{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200173 void* fontResource = NULL;
174 if (mFont)
175 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400176
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200177 // We fill the background
178 gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255);
179 gr_fill(mConsoleX, mConsoleY, mConsoleW, mConsoleH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400180
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200181 gr_color(mScrollColor.red, mScrollColor.green, mScrollColor.blue, mScrollColor.alpha);
182 gr_fill(mConsoleX + (mConsoleW * 9 / 10), mConsoleY, (mConsoleW / 10), mConsoleH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400183
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 // Don't try to continue to render without data
that05360ba2015-02-06 00:58:16 +0100185 size_t prevCount = mLastCount;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200186 mLastCount = gConsole.size();
Dees Troy31218ec2014-02-25 20:35:56 +0000187 mRender = false;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200188 if (mLastCount == 0)
189 return (mSlideout ? RenderSlideout() : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400190
Dees Troy31218ec2014-02-25 20:35:56 +0000191 // Due to word wrap, figure out what / how the newly added text needs to be added to the render vector that is word wrapped
192 // Note, that multiple consoles on different GUI pages may be different widths or use different fonts, so the word wrapping
193 // may different in different console windows
that05360ba2015-02-06 00:58:16 +0100194 for (size_t i = prevCount; i < mLastCount; i++) {
Dees Troy31218ec2014-02-25 20:35:56 +0000195 string curr_line = gConsole[i];
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500196 string curr_color = gConsoleColor[i];
Dees Troy31218ec2014-02-25 20:35:56 +0000197 for(;;) {
that05360ba2015-02-06 00:58:16 +0100198 size_t line_char_width = gr_maxExW(curr_line.c_str(), fontResource, mConsoleW);
Dees Troy31218ec2014-02-25 20:35:56 +0000199 if (line_char_width < curr_line.size()) {
200 rConsole.push_back(curr_line.substr(0, line_char_width));
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500201 rConsoleColor.push_back(curr_color);
Ethan Yonker6b4276f2014-03-07 06:48:45 -0600202 curr_line = curr_line.substr(line_char_width);
Dees Troy31218ec2014-02-25 20:35:56 +0000203 } else {
204 rConsole.push_back(curr_line);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500205 rConsoleColor.push_back(curr_color);
Dees Troy31218ec2014-02-25 20:35:56 +0000206 break;
207 }
208 }
209 }
210 RenderCount = rConsole.size();
211
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 // Find the start point
213 int start;
214 int curLine = mCurrentLine; // Thread-safing (Another thread updates this value)
that05360ba2015-02-06 00:58:16 +0100215 if (curLine == -1) // follow tail
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200216 {
Dees Troy31218ec2014-02-25 20:35:56 +0000217 start = RenderCount - mMaxRows;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200218 }
219 else
220 {
Dees Troy31218ec2014-02-25 20:35:56 +0000221 if (curLine > (int) RenderCount)
222 curLine = (int) RenderCount;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200223 if ((int) mMaxRows > curLine)
224 curLine = (int) mMaxRows;
225 start = curLine - mMaxRows;
226 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400227
that05360ba2015-02-06 00:58:16 +0100228 // note: start can be negative here
229 for (int line = 0; line < mMaxRows; line++)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200230 {
that05360ba2015-02-06 00:58:16 +0100231 int index = start + line;
232 if (index >= 0 && index < (int) RenderCount) {
233 if (rConsoleColor[index] == "normal") {
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500234 gr_color(mForegroundColor.red, mForegroundColor.green, mForegroundColor.blue, mForegroundColor.alpha);
235 } else {
236 COLOR mFontColor;
that05360ba2015-02-06 00:58:16 +0100237 std::string color = rConsoleColor[index];
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500238 ConvertStrToColor(color, &mFontColor);
239 mFontColor.alpha = 255;
240 gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha);
241 }
that05360ba2015-02-06 00:58:16 +0100242 gr_textExW(mConsoleX, mStartY + (line * mFontHeight), rConsole[index].c_str(), fontResource, mConsoleW + mConsoleX);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500243 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200244 }
245 return (mSlideout ? RenderSlideout() : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400246}
247
248int GUIConsole::Render(void)
249{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100250 if(!isConditionTrue())
251 return 0;
252
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253 if (mSlideout && mSlideoutState == hidden)
254 return RenderSlideout();
255
256 return RenderConsole();
Dees_Troy51a0e822012-09-05 15:24:24 -0400257}
258
259int GUIConsole::Update(void)
260{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100261 if(!isConditionTrue())
262 return 0;
263
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 if (mSlideout && mSlideoutState != visible)
265 {
266 if (mSlideoutState == hidden)
267 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400268
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200269 if (mSlideoutState == request_hide)
270 mSlideoutState = hidden;
Dees_Troy51a0e822012-09-05 15:24:24 -0400271
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200272 if (mSlideoutState == request_show)
273 mSlideoutState = visible;
Dees_Troy51a0e822012-09-05 15:24:24 -0400274
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200275 // Any time we activate the slider, we reset the position
276 mCurrentLine = -1;
277 return 2;
278 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400279
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200280 if (mCurrentLine == -1 && mLastCount != gConsole.size())
281 {
282 // We can use Render, and return for just a flip
283 Render();
284 return 2;
285 }
Dees Troy31218ec2014-02-25 20:35:56 +0000286 else if (mRender)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200287 {
288 // They're still touching, so re-render
289 Render();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200290 return 2;
291 }
292 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400293}
294
295int GUIConsole::SetRenderPos(int x, int y, int w, int h)
296{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200297 // Adjust the stub position accordingly
298 mSlideoutX += (x - mConsoleX);
299 mSlideoutY += (y - mConsoleY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400300
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200301 mConsoleX = x;
302 mConsoleY = y;
303 if (w || h)
304 {
305 mConsoleW = w;
306 mConsoleH = h;
307 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400308
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200309 // Calculate the max rows
310 mMaxRows = mConsoleH / mFontHeight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400311
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200312 // Adjust so we always fit to bottom
313 mStartY = mConsoleY + (mConsoleH % mFontHeight);
314 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400315}
316
317// IsInRegion - Checks if the request is handled by this object
thatf8194e22015-01-29 01:05:01 +0100318// Return 1 if this object handles the request, 0 if not
Dees_Troy51a0e822012-09-05 15:24:24 -0400319int GUIConsole::IsInRegion(int x, int y)
320{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200321 if (mSlideout)
322 {
323 // Check if they tapped the slideout button
324 if (x >= mSlideoutX && x <= mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH)
325 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400326
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200327 // If we're only rendering the slideout, bail now
328 if (mSlideoutState == hidden)
329 return 0;
330 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400331
thatf8194e22015-01-29 01:05:01 +0100332 return (x < mConsoleX || x >= mConsoleX + mConsoleW || y < mConsoleY || y >= mConsoleY + mConsoleH) ? 0 : 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400333}
334
335// NotifyTouch - Notify of a touch event
336// Return 0 on success, >0 to ignore remainder of touch, and <0 on error
337int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y)
338{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100339 if(!isConditionTrue())
340 return -1;
341
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200342 if (mSlideout && mSlideoutState == hidden)
343 {
344 if (state == TOUCH_START)
345 {
346 mSlideoutState = request_show;
347 return 1;
348 }
349 }
350 else if (mSlideout && mSlideoutState == visible)
351 {
352 // Are we sliding it back in?
353 if (state == TOUCH_START && x > mSlideoutX && x < (mSlideoutX + mSlideoutW) && y > mSlideoutY && y < (mSlideoutY + mSlideoutH))
354 {
355 mSlideoutState = request_hide;
356 return 1;
357 }
358 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400359
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200360 // If we don't have enough lines to scroll, throw this away.
that05360ba2015-02-06 00:58:16 +0100361 if ((int)RenderCount < mMaxRows) return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400362
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200363 // We are scrolling!!!
364 switch (state)
365 {
366 case TOUCH_START:
367 mLastTouchX = x;
368 mLastTouchY = y;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200369 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400370
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200371 case TOUCH_DRAG:
Dees Troy31218ec2014-02-25 20:35:56 +0000372 if (x < mConsoleX || x > mConsoleX + mConsoleW || y < mConsoleY || y > mConsoleY + mConsoleH)
373 break; // touch is outside of the console area -- do nothing
374 if (y > mLastTouchY + mFontHeight) {
375 while (y > mLastTouchY + mFontHeight) {
376 if (mCurrentLine == -1)
377 mCurrentLine = RenderCount - 1;
378 else if (mCurrentLine > mMaxRows)
379 mCurrentLine--;
380 mLastTouchY += mFontHeight;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200381 }
Dees Troy31218ec2014-02-25 20:35:56 +0000382 mRender = true;
383 } else if (y < mLastTouchY - mFontHeight) {
384 while (y < mLastTouchY - mFontHeight) {
385 if (mCurrentLine >= 0)
386 mCurrentLine++;
387 mLastTouchY -= mFontHeight;
388 }
389 if (mCurrentLine >= (int) RenderCount)
390 mCurrentLine = -1;
391 mRender = true;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200392 }
393 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400394
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200395 case TOUCH_RELEASE:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200396 mLastTouchY = -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400397 case TOUCH_REPEAT:
398 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200399 break;
400 }
401 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400402}