blob: b1b025c48713000481e0c15ab90d26aff1b2575d [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
that8d46c092015-02-26 01:30:04 +0100102GUIConsole::GUIConsole(xml_node<>* node) : GUIScrollList(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 mLastCount = 0;
that8d46c092015-02-26 01:30:04 +0100107 scrollToEnd = true;
108 mSlideoutX = mSlideoutY = mSlideoutW = mSlideoutH = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200109 mSlideout = 0;
that8d46c092015-02-26 01:30:04 +0100110 mSlideoutState = visible;
Dees_Troy51a0e822012-09-05 15:24:24 -0400111
that8d46c092015-02-26 01:30:04 +0100112 allowSelection = false; // console doesn't support list item selections
Dees_Troy51a0e822012-09-05 15:24:24 -0400113
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200114 if (!node)
115 {
that8d46c092015-02-26 01:30:04 +0100116 mRenderX = 0; mRenderY = 0; mRenderW = gr_fb_width(); mRenderH = gr_fb_height();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 }
118 else
119 {
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600120 child = FindNode(node, "color");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200121 if (child)
122 {
that8d46c092015-02-26 01:30:04 +0100123 mFontColor = LoadAttrColor(child, "foreground", mFontColor);
thatf6ed8fc2015-02-14 20:23:16 +0100124 mBackgroundColor = LoadAttrColor(child, "background", mBackgroundColor);
that8d46c092015-02-26 01:30:04 +0100125 //mScrollColor = LoadAttrColor(child, "scroll", mScrollColor);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200126 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400127
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600128 child = FindNode(node, "slideout");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200129 if (child)
130 {
131 mSlideout = 1;
that8d46c092015-02-26 01:30:04 +0100132 mSlideoutState = hidden;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 LoadPlacement(child, &mSlideoutX, &mSlideoutY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
thatf6ed8fc2015-02-14 20:23:16 +0100135 mSlideoutImage = LoadAttrImage(child, "resource");
Dees_Troy51a0e822012-09-05 15:24:24 -0400136
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200137 if (mSlideoutImage && mSlideoutImage->GetResource())
138 {
thatf6ed8fc2015-02-14 20:23:16 +0100139 mSlideoutW = mSlideoutImage->GetWidth();
140 mSlideoutH = mSlideoutImage->GetHeight();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 }
142 }
143 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400144}
145
146int GUIConsole::RenderSlideout(void)
147{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200148 if (!mSlideoutImage || !mSlideoutImage->GetResource())
149 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400150
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY);
152 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400153}
154
that8d46c092015-02-26 01:30:04 +0100155bool GUIConsole::AddLines()
Dees_Troy51a0e822012-09-05 15:24:24 -0400156{
that8d46c092015-02-26 01:30:04 +0100157 if (mLastCount == gConsole.size())
158 return false; // nothing to add
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
that05360ba2015-02-06 00:58:16 +0100160 size_t prevCount = mLastCount;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200161 mLastCount = gConsole.size();
Dees_Troy51a0e822012-09-05 15:24:24 -0400162
Dees Troy31218ec2014-02-25 20:35:56 +0000163 // Due to word wrap, figure out what / how the newly added text needs to be added to the render vector that is word wrapped
164 // Note, that multiple consoles on different GUI pages may be different widths or use different fonts, so the word wrapping
165 // may different in different console windows
that05360ba2015-02-06 00:58:16 +0100166 for (size_t i = prevCount; i < mLastCount; i++) {
Dees Troy31218ec2014-02-25 20:35:56 +0000167 string curr_line = gConsole[i];
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500168 string curr_color = gConsoleColor[i];
Dees Troy31218ec2014-02-25 20:35:56 +0000169 for(;;) {
that8d46c092015-02-26 01:30:04 +0100170 size_t line_char_width = gr_maxExW(curr_line.c_str(), mFont->GetResource(), mRenderW);
Dees Troy31218ec2014-02-25 20:35:56 +0000171 if (line_char_width < curr_line.size()) {
172 rConsole.push_back(curr_line.substr(0, line_char_width));
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500173 rConsoleColor.push_back(curr_color);
Ethan Yonker6b4276f2014-03-07 06:48:45 -0600174 curr_line = curr_line.substr(line_char_width);
Dees Troy31218ec2014-02-25 20:35:56 +0000175 } else {
176 rConsole.push_back(curr_line);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500177 rConsoleColor.push_back(curr_color);
Dees Troy31218ec2014-02-25 20:35:56 +0000178 break;
179 }
180 }
181 }
that8d46c092015-02-26 01:30:04 +0100182 return true;
183}
Dees Troy31218ec2014-02-25 20:35:56 +0000184
that8d46c092015-02-26 01:30:04 +0100185int GUIConsole::RenderConsole(void)
186{
187 AddLines();
188 GUIScrollList::Render();
Dees_Troy51a0e822012-09-05 15:24:24 -0400189
that8d46c092015-02-26 01:30:04 +0100190 // if last line is fully visible, keep tracking the last line when new lines are added
191 int bottom_offset = GetDisplayRemainder() - actualItemHeight;
192 bool isAtBottom = firstDisplayedItem == GetItemCount() - GetDisplayItemCount() - (bottom_offset != 0) && y_offset == bottom_offset;
193 if (isAtBottom)
194 scrollToEnd = true;
195#if 0
196 // debug - show if we are tracking the last line
197 if (scrollToEnd) {
198 gr_color(0,255,0,255);
199 gr_fill(mRenderX+mRenderW-5, mRenderY+mRenderH-5, 5, 5);
200 } else {
201 gr_color(255,0,0,255);
202 gr_fill(mRenderX+mRenderW-5, mRenderY+mRenderH-5, 5, 5);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200203 }
that8d46c092015-02-26 01:30:04 +0100204#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200205 return (mSlideout ? RenderSlideout() : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400206}
207
208int GUIConsole::Render(void)
209{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100210 if(!isConditionTrue())
211 return 0;
212
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 if (mSlideout && mSlideoutState == hidden)
214 return RenderSlideout();
215
216 return RenderConsole();
Dees_Troy51a0e822012-09-05 15:24:24 -0400217}
218
219int GUIConsole::Update(void)
220{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 if (mSlideout && mSlideoutState != visible)
222 {
223 if (mSlideoutState == hidden)
224 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400225
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200226 if (mSlideoutState == request_hide)
227 mSlideoutState = hidden;
Dees_Troy51a0e822012-09-05 15:24:24 -0400228
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200229 if (mSlideoutState == request_show)
230 mSlideoutState = visible;
Dees_Troy51a0e822012-09-05 15:24:24 -0400231
that8d46c092015-02-26 01:30:04 +0100232 // Any time we activate the console, we reset the position
233 SetVisibleListLocation(rConsole.size() - 1);
234 mUpdate = 1;
235 scrollToEnd = true;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200236 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400237
that8d46c092015-02-26 01:30:04 +0100238 if (AddLines()) {
239 // someone added new text
240 // at least the scrollbar must be updated, even if the new lines are currently not visible
241 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200242 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400243
that8d46c092015-02-26 01:30:04 +0100244 if (scrollToEnd) {
245 // keep the last line in view
246 SetVisibleListLocation(rConsole.size() - 1);
247 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400248
that8d46c092015-02-26 01:30:04 +0100249 GUIScrollList::Update();
250
251 if (mUpdate) {
252 mUpdate = 0;
253 if (Render() == 0)
254 return 2;
255 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400257}
258
259// IsInRegion - Checks if the request is handled by this object
thatf8194e22015-01-29 01:05:01 +0100260// Return 1 if this object handles the request, 0 if not
Dees_Troy51a0e822012-09-05 15:24:24 -0400261int GUIConsole::IsInRegion(int x, int y)
262{
that8d46c092015-02-26 01:30:04 +0100263 if (mSlideout) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 // Check if they tapped the slideout button
that8d46c092015-02-26 01:30:04 +0100265 if (x >= mSlideoutX && x < mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200266 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400267
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200268 // If we're only rendering the slideout, bail now
269 if (mSlideoutState == hidden)
270 return 0;
271 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400272
that8d46c092015-02-26 01:30:04 +0100273 return GUIScrollList::IsInRegion(x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400274}
275
276// NotifyTouch - Notify of a touch event
277// Return 0 on success, >0 to ignore remainder of touch, and <0 on error
278int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y)
279{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100280 if(!isConditionTrue())
281 return -1;
282
that8d46c092015-02-26 01:30:04 +0100283 if (mSlideout && x >= mSlideoutX && x < mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH) {
284 if (state == TOUCH_START) {
285 if (mSlideoutState == hidden)
286 mSlideoutState = request_show;
287 else if (mSlideoutState == visible)
288 mSlideoutState = request_hide;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200289 }
that8d46c092015-02-26 01:30:04 +0100290 return 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200291 }
that8d46c092015-02-26 01:30:04 +0100292 scrollToEnd = false;
293 return GUIScrollList::NotifyTouch(state, x, y);
294}
295
296size_t GUIConsole::GetItemCount()
297{
298 return rConsole.size();
299}
300
301void GUIConsole::RenderItem(size_t itemindex, int yPos, bool selected)
302{
303 // Set the color for the font
304 if (rConsoleColor[itemindex] == "normal") {
305 gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha);
306 } else {
307 COLOR FontColor;
308 std::string color = rConsoleColor[itemindex];
309 ConvertStrToColor(color, &FontColor);
310 FontColor.alpha = 255;
311 gr_color(FontColor.red, FontColor.green, FontColor.blue, FontColor.alpha);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200312 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400313
that8d46c092015-02-26 01:30:04 +0100314 // render text
315 const char* text = rConsole[itemindex].c_str();
316 gr_textEx(mRenderX, yPos, text, mFont->GetResource());
317}
Dees_Troy51a0e822012-09-05 15:24:24 -0400318
that8d46c092015-02-26 01:30:04 +0100319void GUIConsole::NotifySelect(size_t item_selected)
320{
321 // do nothing - console ignores selections
Dees_Troy51a0e822012-09-05 15:24:24 -0400322}