blob: bb70400c91b7df9c511cd0ad835a3225feadcf32 [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 {
thatf6ed8fc2015-02-14 20:23:16 +0100132 mFont = LoadAttrFont(child, "resource");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 child = node->first_node("color");
136 if (child)
137 {
thatf6ed8fc2015-02-14 20:23:16 +0100138 mForegroundColor = LoadAttrColor(child, "foreground", mForegroundColor);
139 mBackgroundColor = LoadAttrColor(child, "background", mBackgroundColor);
140 mScrollColor = LoadAttrColor(child, "scroll", mScrollColor);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400142
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200143 // Load the placement
144 LoadPlacement(node->first_node("placement"), &mConsoleX, &mConsoleY, &mConsoleW, &mConsoleH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400145
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200146 child = node->first_node("slideout");
147 if (child)
148 {
149 mSlideout = 1;
150 LoadPlacement(child, &mSlideoutX, &mSlideoutY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400151
thatf6ed8fc2015-02-14 20:23:16 +0100152 mSlideoutImage = LoadAttrImage(child, "resource");
Dees_Troy51a0e822012-09-05 15:24:24 -0400153
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 if (mSlideoutImage && mSlideoutImage->GetResource())
155 {
thatf6ed8fc2015-02-14 20:23:16 +0100156 mSlideoutW = mSlideoutImage->GetWidth();
157 mSlideoutH = mSlideoutImage->GetHeight();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 }
159 }
160 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400161
thatf6ed8fc2015-02-14 20:23:16 +0100162 mFontHeight = mFont->GetHeight();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
164 SetRenderPos(mConsoleX, mConsoleY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400165}
166
167int GUIConsole::RenderSlideout(void)
168{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200169 if (!mSlideoutImage || !mSlideoutImage->GetResource())
170 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400171
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200172 gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY);
173 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400174}
175
176int GUIConsole::RenderConsole(void)
177{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200178 void* fontResource = NULL;
179 if (mFont)
180 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400181
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200182 // We fill the background
183 gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255);
184 gr_fill(mConsoleX, mConsoleY, mConsoleW, mConsoleH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400185
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200186 gr_color(mScrollColor.red, mScrollColor.green, mScrollColor.blue, mScrollColor.alpha);
187 gr_fill(mConsoleX + (mConsoleW * 9 / 10), mConsoleY, (mConsoleW / 10), mConsoleH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400188
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200189 // Don't try to continue to render without data
that05360ba2015-02-06 00:58:16 +0100190 size_t prevCount = mLastCount;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200191 mLastCount = gConsole.size();
Dees Troy31218ec2014-02-25 20:35:56 +0000192 mRender = false;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200193 if (mLastCount == 0)
194 return (mSlideout ? RenderSlideout() : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400195
Dees Troy31218ec2014-02-25 20:35:56 +0000196 // Due to word wrap, figure out what / how the newly added text needs to be added to the render vector that is word wrapped
197 // Note, that multiple consoles on different GUI pages may be different widths or use different fonts, so the word wrapping
198 // may different in different console windows
that05360ba2015-02-06 00:58:16 +0100199 for (size_t i = prevCount; i < mLastCount; i++) {
Dees Troy31218ec2014-02-25 20:35:56 +0000200 string curr_line = gConsole[i];
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500201 string curr_color = gConsoleColor[i];
Dees Troy31218ec2014-02-25 20:35:56 +0000202 for(;;) {
that05360ba2015-02-06 00:58:16 +0100203 size_t line_char_width = gr_maxExW(curr_line.c_str(), fontResource, mConsoleW);
Dees Troy31218ec2014-02-25 20:35:56 +0000204 if (line_char_width < curr_line.size()) {
205 rConsole.push_back(curr_line.substr(0, line_char_width));
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500206 rConsoleColor.push_back(curr_color);
Ethan Yonker6b4276f2014-03-07 06:48:45 -0600207 curr_line = curr_line.substr(line_char_width);
Dees Troy31218ec2014-02-25 20:35:56 +0000208 } else {
209 rConsole.push_back(curr_line);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500210 rConsoleColor.push_back(curr_color);
Dees Troy31218ec2014-02-25 20:35:56 +0000211 break;
212 }
213 }
214 }
215 RenderCount = rConsole.size();
216
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200217 // Find the start point
218 int start;
219 int curLine = mCurrentLine; // Thread-safing (Another thread updates this value)
that05360ba2015-02-06 00:58:16 +0100220 if (curLine == -1) // follow tail
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 {
Dees Troy31218ec2014-02-25 20:35:56 +0000222 start = RenderCount - mMaxRows;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200223 }
224 else
225 {
Dees Troy31218ec2014-02-25 20:35:56 +0000226 if (curLine > (int) RenderCount)
227 curLine = (int) RenderCount;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200228 if ((int) mMaxRows > curLine)
229 curLine = (int) mMaxRows;
230 start = curLine - mMaxRows;
231 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400232
that05360ba2015-02-06 00:58:16 +0100233 // note: start can be negative here
234 for (int line = 0; line < mMaxRows; line++)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 {
that05360ba2015-02-06 00:58:16 +0100236 int index = start + line;
237 if (index >= 0 && index < (int) RenderCount) {
238 if (rConsoleColor[index] == "normal") {
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500239 gr_color(mForegroundColor.red, mForegroundColor.green, mForegroundColor.blue, mForegroundColor.alpha);
240 } else {
241 COLOR mFontColor;
that05360ba2015-02-06 00:58:16 +0100242 std::string color = rConsoleColor[index];
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500243 ConvertStrToColor(color, &mFontColor);
244 mFontColor.alpha = 255;
245 gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha);
246 }
that05360ba2015-02-06 00:58:16 +0100247 gr_textExW(mConsoleX, mStartY + (line * mFontHeight), rConsole[index].c_str(), fontResource, mConsoleW + mConsoleX);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500248 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 }
250 return (mSlideout ? RenderSlideout() : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400251}
252
253int GUIConsole::Render(void)
254{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100255 if(!isConditionTrue())
256 return 0;
257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 if (mSlideout && mSlideoutState == hidden)
259 return RenderSlideout();
260
261 return RenderConsole();
Dees_Troy51a0e822012-09-05 15:24:24 -0400262}
263
264int GUIConsole::Update(void)
265{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100266 if(!isConditionTrue())
267 return 0;
268
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200269 if (mSlideout && mSlideoutState != visible)
270 {
271 if (mSlideoutState == hidden)
272 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400273
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200274 if (mSlideoutState == request_hide)
275 mSlideoutState = hidden;
Dees_Troy51a0e822012-09-05 15:24:24 -0400276
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200277 if (mSlideoutState == request_show)
278 mSlideoutState = visible;
Dees_Troy51a0e822012-09-05 15:24:24 -0400279
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200280 // Any time we activate the slider, we reset the position
281 mCurrentLine = -1;
282 return 2;
283 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400284
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200285 if (mCurrentLine == -1 && mLastCount != gConsole.size())
286 {
287 // We can use Render, and return for just a flip
288 Render();
289 return 2;
290 }
Dees Troy31218ec2014-02-25 20:35:56 +0000291 else if (mRender)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200292 {
293 // They're still touching, so re-render
294 Render();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 return 2;
296 }
297 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400298}
299
300int GUIConsole::SetRenderPos(int x, int y, int w, int h)
301{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200302 // Adjust the stub position accordingly
303 mSlideoutX += (x - mConsoleX);
304 mSlideoutY += (y - mConsoleY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400305
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200306 mConsoleX = x;
307 mConsoleY = y;
308 if (w || h)
309 {
310 mConsoleW = w;
311 mConsoleH = h;
312 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400313
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200314 // Calculate the max rows
315 mMaxRows = mConsoleH / mFontHeight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400316
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200317 // Adjust so we always fit to bottom
318 mStartY = mConsoleY + (mConsoleH % mFontHeight);
319 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400320}
321
322// IsInRegion - Checks if the request is handled by this object
thatf8194e22015-01-29 01:05:01 +0100323// Return 1 if this object handles the request, 0 if not
Dees_Troy51a0e822012-09-05 15:24:24 -0400324int GUIConsole::IsInRegion(int x, int y)
325{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200326 if (mSlideout)
327 {
328 // Check if they tapped the slideout button
329 if (x >= mSlideoutX && x <= mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH)
330 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400331
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200332 // If we're only rendering the slideout, bail now
333 if (mSlideoutState == hidden)
334 return 0;
335 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400336
thatf8194e22015-01-29 01:05:01 +0100337 return (x < mConsoleX || x >= mConsoleX + mConsoleW || y < mConsoleY || y >= mConsoleY + mConsoleH) ? 0 : 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400338}
339
340// NotifyTouch - Notify of a touch event
341// Return 0 on success, >0 to ignore remainder of touch, and <0 on error
342int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y)
343{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100344 if(!isConditionTrue())
345 return -1;
346
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200347 if (mSlideout && mSlideoutState == hidden)
348 {
349 if (state == TOUCH_START)
350 {
351 mSlideoutState = request_show;
352 return 1;
353 }
354 }
355 else if (mSlideout && mSlideoutState == visible)
356 {
357 // Are we sliding it back in?
358 if (state == TOUCH_START && x > mSlideoutX && x < (mSlideoutX + mSlideoutW) && y > mSlideoutY && y < (mSlideoutY + mSlideoutH))
359 {
360 mSlideoutState = request_hide;
361 return 1;
362 }
363 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400364
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200365 // If we don't have enough lines to scroll, throw this away.
that05360ba2015-02-06 00:58:16 +0100366 if ((int)RenderCount < mMaxRows) return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400367
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200368 // We are scrolling!!!
369 switch (state)
370 {
371 case TOUCH_START:
372 mLastTouchX = x;
373 mLastTouchY = y;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200374 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200376 case TOUCH_DRAG:
Dees Troy31218ec2014-02-25 20:35:56 +0000377 if (x < mConsoleX || x > mConsoleX + mConsoleW || y < mConsoleY || y > mConsoleY + mConsoleH)
378 break; // touch is outside of the console area -- do nothing
379 if (y > mLastTouchY + mFontHeight) {
380 while (y > mLastTouchY + mFontHeight) {
381 if (mCurrentLine == -1)
382 mCurrentLine = RenderCount - 1;
383 else if (mCurrentLine > mMaxRows)
384 mCurrentLine--;
385 mLastTouchY += mFontHeight;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200386 }
Dees Troy31218ec2014-02-25 20:35:56 +0000387 mRender = true;
388 } else if (y < mLastTouchY - mFontHeight) {
389 while (y < mLastTouchY - mFontHeight) {
390 if (mCurrentLine >= 0)
391 mCurrentLine++;
392 mLastTouchY -= mFontHeight;
393 }
394 if (mCurrentLine >= (int) RenderCount)
395 mCurrentLine = -1;
396 mRender = true;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200397 }
398 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400399
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200400 case TOUCH_RELEASE:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200401 mLastTouchY = -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400402 case TOUCH_REPEAT:
403 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200404 break;
405 }
406 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400407}