blob: 5d0ed3ec3a2d4df4da1af42f09e3451dc431acca [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;
30
31extern "C" void gui_print(const char *fmt, ...)
32{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020033 char buf[512]; // We're going to limit a single request to 512 bytes
Dees_Troy51a0e822012-09-05 15:24:24 -040034
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020035 va_list ap;
36 va_start(ap, fmt);
37 vsnprintf(buf, 512, fmt, ap);
38 va_end(ap);
Dees_Troy51a0e822012-09-05 15:24:24 -040039
Dees_Troy32c8eb82012-09-11 15:28:06 -040040 fputs(buf, stdout);
41
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020042 char *start, *next;
Dees_Troy51a0e822012-09-05 15:24:24 -040043
44 if (buf[0] == '\n' && strlen(buf) < 2) {
45 // This prevents the double lines bug seen in the console during zip installs
46 return;
47 }
48
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020049 for (start = next = buf; *next != '\0'; next++)
50 {
51 if (*next == '\n')
52 {
53 *next = '\0';
54 next++;
Dees_Troy51a0e822012-09-05 15:24:24 -040055
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020056 std::string line = start;
57 gConsole.push_back(line);
58 start = next;
Dees_Troy51a0e822012-09-05 15:24:24 -040059
60 // Handle the normal \n\0 case
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020061 if (*next == '\0')
Dees_Troy51a0e822012-09-05 15:24:24 -040062 return;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020063 }
64 }
65 std::string line = start;
66 gConsole.push_back(line);
67 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040068}
69
70extern "C" void gui_print_overwrite(const char *fmt, ...)
71{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020072 char buf[512]; // We're going to limit a single request to 512 bytes
Dees_Troy51a0e822012-09-05 15:24:24 -040073
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020074 va_list ap;
75 va_start(ap, fmt);
76 vsnprintf(buf, 512, fmt, ap);
77 va_end(ap);
Dees_Troy51a0e822012-09-05 15:24:24 -040078
Dees_Troy32c8eb82012-09-11 15:28:06 -040079 fputs(buf, stdout);
80
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020081 // Pop the last line, and we can continue
82 if (!gConsole.empty()) gConsole.pop_back();
Dees_Troy51a0e822012-09-05 15:24:24 -040083
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020084 char *start, *next;
85 for (start = next = buf; *next != '\0'; next++)
86 {
87 if (*next == '\n')
88 {
89 *next = '\0';
90 next++;
91
92 std::string line = start;
93 gConsole.push_back(line);
94 start = next;
Dees_Troy51a0e822012-09-05 15:24:24 -040095
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020096 // Handle the normal \n\0 case
97 if (*next == '\0')
Dees_Troy51a0e822012-09-05 15:24:24 -040098 return;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099 }
100 }
101 std::string line = start;
102 gConsole.push_back(line);
103 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400104}
105
Vojtech Bocekede51c52014-02-07 23:58:09 +0100106GUIConsole::GUIConsole(xml_node<>* node) : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400107{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200108 xml_attribute<>* attr;
109 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -0400110
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200111 mFont = NULL;
112 mCurrentLine = -1;
113 memset(&mForegroundColor, 255, sizeof(COLOR));
114 memset(&mBackgroundColor, 0, sizeof(COLOR));
115 mBackgroundColor.alpha = 255;
116 memset(&mScrollColor, 0x08, sizeof(COLOR));
117 mScrollColor.alpha = 255;
118 mLastCount = 0;
119 mSlideout = 0;
120 mSlideoutState = hidden;
Dees_Troy51a0e822012-09-05 15:24:24 -0400121
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200122 mRenderX = 0; mRenderY = 0; mRenderW = gr_fb_width(); mRenderH = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400123
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200124 if (!node)
125 {
126 mSlideoutX = 0; mSlideoutY = 0; mSlideoutW = 0; mSlideoutH = 0;
127 mConsoleX = 0; mConsoleY = 0; mConsoleW = gr_fb_width(); mConsoleH = gr_fb_height();
128 }
129 else
130 {
131 child = node->first_node("font");
132 if (child)
133 {
134 attr = child->first_attribute("resource");
135 if (attr)
136 mFont = PageManager::FindResource(attr->value());
137 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400138
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200139 child = node->first_node("color");
140 if (child)
141 {
142 attr = child->first_attribute("foreground");
143 if (attr)
144 {
145 std::string color = attr->value();
146 ConvertStrToColor(color, &mForegroundColor);
147 }
148 attr = child->first_attribute("background");
149 if (attr)
150 {
151 std::string color = attr->value();
152 ConvertStrToColor(color, &mBackgroundColor);
153 }
154 attr = child->first_attribute("scroll");
155 if (attr)
156 {
157 std::string color = attr->value();
158 ConvertStrToColor(color, &mScrollColor);
159 }
160 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400161
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200162 // Load the placement
163 LoadPlacement(node->first_node("placement"), &mConsoleX, &mConsoleY, &mConsoleW, &mConsoleH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400164
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200165 child = node->first_node("slideout");
166 if (child)
167 {
168 mSlideout = 1;
169 LoadPlacement(child, &mSlideoutX, &mSlideoutY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400170
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200171 attr = child->first_attribute("resource");
172 if (attr) mSlideoutImage = PageManager::FindResource(attr->value());
Dees_Troy51a0e822012-09-05 15:24:24 -0400173
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200174 if (mSlideoutImage && mSlideoutImage->GetResource())
175 {
176 mSlideoutW = gr_get_width(mSlideoutImage->GetResource());
177 mSlideoutH = gr_get_height(mSlideoutImage->GetResource());
178 }
179 }
180 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400181
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200182 gr_getFontDetails(mFont, &mFontHeight, NULL);
183 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
184 SetRenderPos(mConsoleX, mConsoleY);
185 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400186}
187
188int GUIConsole::RenderSlideout(void)
189{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200190 if (!mSlideoutImage || !mSlideoutImage->GetResource())
191 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400192
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200193 gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY);
194 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400195}
196
197int GUIConsole::RenderConsole(void)
198{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200199 void* fontResource = NULL;
200 if (mFont)
201 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400202
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200203 // We fill the background
204 gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255);
205 gr_fill(mConsoleX, mConsoleY, mConsoleW, mConsoleH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400206
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200207 gr_color(mScrollColor.red, mScrollColor.green, mScrollColor.blue, mScrollColor.alpha);
208 gr_fill(mConsoleX + (mConsoleW * 9 / 10), mConsoleY, (mConsoleW / 10), mConsoleH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400209
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200210 // Render the lines
211 gr_color(mForegroundColor.red, mForegroundColor.green, mForegroundColor.blue, mForegroundColor.alpha);
Dees_Troy51a0e822012-09-05 15:24:24 -0400212
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 // Don't try to continue to render without data
214 mLastCount = gConsole.size();
215 if (mLastCount == 0)
216 return (mSlideout ? RenderSlideout() : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400217
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200218 // Find the start point
219 int start;
220 int curLine = mCurrentLine; // Thread-safing (Another thread updates this value)
221 if (curLine == -1)
222 {
223 start = mLastCount - mMaxRows;
224 }
225 else
226 {
227 if (curLine > (int) mLastCount)
228 curLine = (int) mLastCount;
229 if ((int) mMaxRows > curLine)
230 curLine = (int) mMaxRows;
231 start = curLine - mMaxRows;
232 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400233
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200234 unsigned int line;
235 for (line = 0; line < mMaxRows; line++)
236 {
237 if ((start + (int) line) >= 0 && (start + (int) line) < (int) mLastCount)
238 gr_textExW(mConsoleX, mStartY + (line * mFontHeight), gConsole[start + line].c_str(), fontResource, mConsoleW + mConsoleX);
239 }
240 return (mSlideout ? RenderSlideout() : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400241}
242
243int GUIConsole::Render(void)
244{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100245 if(!isConditionTrue())
246 return 0;
247
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 if (mSlideout && mSlideoutState == hidden)
249 return RenderSlideout();
250
251 return RenderConsole();
Dees_Troy51a0e822012-09-05 15:24:24 -0400252}
253
254int GUIConsole::Update(void)
255{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100256 if(!isConditionTrue())
257 return 0;
258
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200259 if (mSlideout && mSlideoutState != visible)
260 {
261 if (mSlideoutState == hidden)
262 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400263
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 if (mSlideoutState == request_hide)
265 mSlideoutState = hidden;
Dees_Troy51a0e822012-09-05 15:24:24 -0400266
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200267 if (mSlideoutState == request_show)
268 mSlideoutState = visible;
Dees_Troy51a0e822012-09-05 15:24:24 -0400269
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200270 // Any time we activate the slider, we reset the position
271 mCurrentLine = -1;
272 return 2;
273 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400274
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200275 if (mCurrentLine == -1 && mLastCount != gConsole.size())
276 {
277 // We can use Render, and return for just a flip
278 Render();
279 return 2;
280 }
281 else if (mLastTouchY >= 0)
282 {
283 // They're still touching, so re-render
284 Render();
285 mLastTouchY = -1;
286 return 2;
287 }
288 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400289}
290
291int GUIConsole::SetRenderPos(int x, int y, int w, int h)
292{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200293 // Adjust the stub position accordingly
294 mSlideoutX += (x - mConsoleX);
295 mSlideoutY += (y - mConsoleY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400296
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200297 mConsoleX = x;
298 mConsoleY = y;
299 if (w || h)
300 {
301 mConsoleW = w;
302 mConsoleH = h;
303 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400304
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200305 // Calculate the max rows
306 mMaxRows = mConsoleH / mFontHeight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400307
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200308 // Adjust so we always fit to bottom
309 mStartY = mConsoleY + (mConsoleH % mFontHeight);
310 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400311}
312
313// IsInRegion - Checks if the request is handled by this object
314// Return 0 if this object handles the request, 1 if not
315int GUIConsole::IsInRegion(int x, int y)
316{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200317 if (mSlideout)
318 {
319 // Check if they tapped the slideout button
320 if (x >= mSlideoutX && x <= mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH)
321 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400322
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200323 // If we're only rendering the slideout, bail now
324 if (mSlideoutState == hidden)
325 return 0;
326 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400327
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200328 return (x < mConsoleX || x > mConsoleX + mConsoleW || y < mConsoleY || y > mConsoleY + mConsoleH) ? 0 : 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400329}
330
331// NotifyTouch - Notify of a touch event
332// Return 0 on success, >0 to ignore remainder of touch, and <0 on error
333int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y)
334{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100335 if(!isConditionTrue())
336 return -1;
337
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200338 if (mSlideout && mSlideoutState == hidden)
339 {
340 if (state == TOUCH_START)
341 {
342 mSlideoutState = request_show;
343 return 1;
344 }
345 }
346 else if (mSlideout && mSlideoutState == visible)
347 {
348 // Are we sliding it back in?
349 if (state == TOUCH_START && x > mSlideoutX && x < (mSlideoutX + mSlideoutW) && y > mSlideoutY && y < (mSlideoutY + mSlideoutH))
350 {
351 mSlideoutState = request_hide;
352 return 1;
353 }
354 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400355
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200356 // If we don't have enough lines to scroll, throw this away.
357 if (mLastCount < mMaxRows) return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400358
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200359 // We are scrolling!!!
360 switch (state)
361 {
362 case TOUCH_START:
363 mLastTouchX = x;
364 mLastTouchY = y;
365 if ((x - mConsoleX) > ((9 * mConsoleW) / 10))
366 mSlideMultiplier = 10;
367 else
368 mSlideMultiplier = 1;
369 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400370
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200371 case TOUCH_DRAG:
372 // This handles tapping
373 if (x == mLastTouchX && y == mLastTouchY) break;
374 mLastTouchX = -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200376 if (y > mLastTouchY + 5)
377 {
378 mLastTouchY = y;
379 if (mCurrentLine == -1)
380 mCurrentLine = mLastCount - 1;
381 else if (mCurrentLine > mSlideMultiplier)
382 mCurrentLine -= mSlideMultiplier;
383 else
384 mCurrentLine = mMaxRows;
Dees_Troy51a0e822012-09-05 15:24:24 -0400385
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200386 if (mCurrentLine < (int) mMaxRows)
387 mCurrentLine = mMaxRows;
388 }
389 else if (y < mLastTouchY - 5)
390 {
391 mLastTouchY = y;
392 if (mCurrentLine >= 0)
393 {
394 mCurrentLine += mSlideMultiplier;
395 if (mCurrentLine >= (int) mLastCount)
396 mCurrentLine = -1;
397 }
398 }
399 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400400
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200401 case TOUCH_RELEASE:
402 // On a tap, we jump to the tail
403 if (mLastTouchX >= 0)
404 mCurrentLine = -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400405
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200406 mLastTouchY = -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400407 case TOUCH_REPEAT:
408 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200409 break;
410 }
411 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400412}