blob: aad392ca38239438734de8eff2275e6b2e0bbf7a [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_attribute<>* attr;
105 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -0400106
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200107 mFont = NULL;
108 mCurrentLine = -1;
109 memset(&mForegroundColor, 255, sizeof(COLOR));
110 memset(&mBackgroundColor, 0, sizeof(COLOR));
111 mBackgroundColor.alpha = 255;
112 memset(&mScrollColor, 0x08, sizeof(COLOR));
113 mScrollColor.alpha = 255;
114 mLastCount = 0;
115 mSlideout = 0;
Dees Troy31218ec2014-02-25 20:35:56 +0000116 RenderCount = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 mSlideoutState = hidden;
Dees Troy31218ec2014-02-25 20:35:56 +0000118 mRender = true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 mRenderX = 0; mRenderY = 0; mRenderW = gr_fb_width(); mRenderH = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400121
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200122 if (!node)
123 {
124 mSlideoutX = 0; mSlideoutY = 0; mSlideoutW = 0; mSlideoutH = 0;
125 mConsoleX = 0; mConsoleY = 0; mConsoleW = gr_fb_width(); mConsoleH = gr_fb_height();
126 }
127 else
128 {
129 child = node->first_node("font");
130 if (child)
131 {
132 attr = child->first_attribute("resource");
133 if (attr)
134 mFont = PageManager::FindResource(attr->value());
135 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400136
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200137 child = node->first_node("color");
138 if (child)
139 {
140 attr = child->first_attribute("foreground");
141 if (attr)
142 {
143 std::string color = attr->value();
144 ConvertStrToColor(color, &mForegroundColor);
145 }
146 attr = child->first_attribute("background");
147 if (attr)
148 {
149 std::string color = attr->value();
150 ConvertStrToColor(color, &mBackgroundColor);
151 }
152 attr = child->first_attribute("scroll");
153 if (attr)
154 {
155 std::string color = attr->value();
156 ConvertStrToColor(color, &mScrollColor);
157 }
158 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 // Load the placement
161 LoadPlacement(node->first_node("placement"), &mConsoleX, &mConsoleY, &mConsoleW, &mConsoleH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400162
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 child = node->first_node("slideout");
164 if (child)
165 {
166 mSlideout = 1;
167 LoadPlacement(child, &mSlideoutX, &mSlideoutY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400168
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200169 attr = child->first_attribute("resource");
170 if (attr) mSlideoutImage = PageManager::FindResource(attr->value());
Dees_Troy51a0e822012-09-05 15:24:24 -0400171
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200172 if (mSlideoutImage && mSlideoutImage->GetResource())
173 {
174 mSlideoutW = gr_get_width(mSlideoutImage->GetResource());
175 mSlideoutH = gr_get_height(mSlideoutImage->GetResource());
176 }
177 }
178 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400179
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200180 mFontHeight = gr_getMaxFontHeight(mFont ? mFont->GetResource() : NULL);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200181 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
182 SetRenderPos(mConsoleX, mConsoleY);
183 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400184}
185
186int GUIConsole::RenderSlideout(void)
187{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200188 if (!mSlideoutImage || !mSlideoutImage->GetResource())
189 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400190
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200191 gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY);
192 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400193}
194
195int GUIConsole::RenderConsole(void)
196{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200197 void* fontResource = NULL;
198 if (mFont)
199 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400200
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200201 // We fill the background
202 gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255);
203 gr_fill(mConsoleX, mConsoleY, mConsoleW, mConsoleH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400204
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200205 gr_color(mScrollColor.red, mScrollColor.green, mScrollColor.blue, mScrollColor.alpha);
206 gr_fill(mConsoleX + (mConsoleW * 9 / 10), mConsoleY, (mConsoleW / 10), mConsoleH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400207
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200208 // Don't try to continue to render without data
Dees Troy31218ec2014-02-25 20:35:56 +0000209 int prevCount = mLastCount;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200210 mLastCount = gConsole.size();
Dees Troy31218ec2014-02-25 20:35:56 +0000211 mRender = false;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 if (mLastCount == 0)
213 return (mSlideout ? RenderSlideout() : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400214
Dees Troy31218ec2014-02-25 20:35:56 +0000215 // Due to word wrap, figure out what / how the newly added text needs to be added to the render vector that is word wrapped
216 // Note, that multiple consoles on different GUI pages may be different widths or use different fonts, so the word wrapping
217 // may different in different console windows
218 for (int i = prevCount; i < mLastCount; i++) {
219 string curr_line = gConsole[i];
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500220 string curr_color = gConsoleColor[i];
Dees Troy31218ec2014-02-25 20:35:56 +0000221 int line_char_width;
222 for(;;) {
223 line_char_width = gr_maxExW(curr_line.c_str(), fontResource, mConsoleW);
224 if (line_char_width < curr_line.size()) {
225 rConsole.push_back(curr_line.substr(0, line_char_width));
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500226 rConsoleColor.push_back(curr_color);
Ethan Yonker6b4276f2014-03-07 06:48:45 -0600227 curr_line = curr_line.substr(line_char_width);
Dees Troy31218ec2014-02-25 20:35:56 +0000228 } else {
229 rConsole.push_back(curr_line);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500230 rConsoleColor.push_back(curr_color);
Dees Troy31218ec2014-02-25 20:35:56 +0000231 break;
232 }
233 }
234 }
235 RenderCount = rConsole.size();
236
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200237 // Find the start point
238 int start;
239 int curLine = mCurrentLine; // Thread-safing (Another thread updates this value)
240 if (curLine == -1)
241 {
Dees Troy31218ec2014-02-25 20:35:56 +0000242 start = RenderCount - mMaxRows;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200243 }
244 else
245 {
Dees Troy31218ec2014-02-25 20:35:56 +0000246 if (curLine > (int) RenderCount)
247 curLine = (int) RenderCount;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 if ((int) mMaxRows > curLine)
249 curLine = (int) mMaxRows;
250 start = curLine - mMaxRows;
251 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400252
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253 unsigned int line;
254 for (line = 0; line < mMaxRows; line++)
255 {
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500256 if ((start + (int) line) >= 0 && (start + (int) line) < (int) RenderCount) {
257 if (rConsoleColor[start + line] == "normal") {
258 gr_color(mForegroundColor.red, mForegroundColor.green, mForegroundColor.blue, mForegroundColor.alpha);
259 } else {
260 COLOR mFontColor;
261 std::string color = rConsoleColor[start + line];
262 ConvertStrToColor(color, &mFontColor);
263 mFontColor.alpha = 255;
264 gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha);
265 }
Dees Troy31218ec2014-02-25 20:35:56 +0000266 gr_textExW(mConsoleX, mStartY + (line * mFontHeight), rConsole[start + line].c_str(), fontResource, mConsoleW + mConsoleX);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500267 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200268 }
269 return (mSlideout ? RenderSlideout() : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400270}
271
272int GUIConsole::Render(void)
273{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100274 if(!isConditionTrue())
275 return 0;
276
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200277 if (mSlideout && mSlideoutState == hidden)
278 return RenderSlideout();
279
280 return RenderConsole();
Dees_Troy51a0e822012-09-05 15:24:24 -0400281}
282
283int GUIConsole::Update(void)
284{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100285 if(!isConditionTrue())
286 return 0;
287
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200288 if (mSlideout && mSlideoutState != visible)
289 {
290 if (mSlideoutState == hidden)
291 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400292
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200293 if (mSlideoutState == request_hide)
294 mSlideoutState = hidden;
Dees_Troy51a0e822012-09-05 15:24:24 -0400295
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200296 if (mSlideoutState == request_show)
297 mSlideoutState = visible;
Dees_Troy51a0e822012-09-05 15:24:24 -0400298
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200299 // Any time we activate the slider, we reset the position
300 mCurrentLine = -1;
301 return 2;
302 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400303
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200304 if (mCurrentLine == -1 && mLastCount != gConsole.size())
305 {
306 // We can use Render, and return for just a flip
307 Render();
308 return 2;
309 }
Dees Troy31218ec2014-02-25 20:35:56 +0000310 else if (mRender)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200311 {
312 // They're still touching, so re-render
313 Render();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200314 return 2;
315 }
316 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400317}
318
319int GUIConsole::SetRenderPos(int x, int y, int w, int h)
320{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200321 // Adjust the stub position accordingly
322 mSlideoutX += (x - mConsoleX);
323 mSlideoutY += (y - mConsoleY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400324
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200325 mConsoleX = x;
326 mConsoleY = y;
327 if (w || h)
328 {
329 mConsoleW = w;
330 mConsoleH = h;
331 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400332
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200333 // Calculate the max rows
334 mMaxRows = mConsoleH / mFontHeight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400335
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200336 // Adjust so we always fit to bottom
337 mStartY = mConsoleY + (mConsoleH % mFontHeight);
338 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400339}
340
341// IsInRegion - Checks if the request is handled by this object
342// Return 0 if this object handles the request, 1 if not
343int GUIConsole::IsInRegion(int x, int y)
344{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200345 if (mSlideout)
346 {
347 // Check if they tapped the slideout button
348 if (x >= mSlideoutX && x <= mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH)
349 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400350
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200351 // If we're only rendering the slideout, bail now
352 if (mSlideoutState == hidden)
353 return 0;
354 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400355
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200356 return (x < mConsoleX || x > mConsoleX + mConsoleW || y < mConsoleY || y > mConsoleY + mConsoleH) ? 0 : 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400357}
358
359// NotifyTouch - Notify of a touch event
360// Return 0 on success, >0 to ignore remainder of touch, and <0 on error
361int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y)
362{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100363 if(!isConditionTrue())
364 return -1;
365
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200366 if (mSlideout && mSlideoutState == hidden)
367 {
368 if (state == TOUCH_START)
369 {
370 mSlideoutState = request_show;
371 return 1;
372 }
373 }
374 else if (mSlideout && mSlideoutState == visible)
375 {
376 // Are we sliding it back in?
377 if (state == TOUCH_START && x > mSlideoutX && x < (mSlideoutX + mSlideoutW) && y > mSlideoutY && y < (mSlideoutY + mSlideoutH))
378 {
379 mSlideoutState = request_hide;
380 return 1;
381 }
382 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400383
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200384 // If we don't have enough lines to scroll, throw this away.
Dees Troy31218ec2014-02-25 20:35:56 +0000385 if (RenderCount < mMaxRows) return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400386
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200387 // We are scrolling!!!
388 switch (state)
389 {
390 case TOUCH_START:
391 mLastTouchX = x;
392 mLastTouchY = y;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200393 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400394
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200395 case TOUCH_DRAG:
Dees Troy31218ec2014-02-25 20:35:56 +0000396 if (x < mConsoleX || x > mConsoleX + mConsoleW || y < mConsoleY || y > mConsoleY + mConsoleH)
397 break; // touch is outside of the console area -- do nothing
398 if (y > mLastTouchY + mFontHeight) {
399 while (y > mLastTouchY + mFontHeight) {
400 if (mCurrentLine == -1)
401 mCurrentLine = RenderCount - 1;
402 else if (mCurrentLine > mMaxRows)
403 mCurrentLine--;
404 mLastTouchY += mFontHeight;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200405 }
Dees Troy31218ec2014-02-25 20:35:56 +0000406 mRender = true;
407 } else if (y < mLastTouchY - mFontHeight) {
408 while (y < mLastTouchY - mFontHeight) {
409 if (mCurrentLine >= 0)
410 mCurrentLine++;
411 mLastTouchY -= mFontHeight;
412 }
413 if (mCurrentLine >= (int) RenderCount)
414 mCurrentLine = -1;
415 mRender = true;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200416 }
417 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400418
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200419 case TOUCH_RELEASE:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200420 mLastTouchY = -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400421 case TOUCH_REPEAT:
422 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200423 break;
424 }
425 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400426}