Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // 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 | |
| 20 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 21 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | #include "rapidxml.hpp" |
| 26 | #include "objects.hpp" |
| 27 | |
| 28 | |
| 29 | static std::vector<std::string> gConsole; |
| 30 | |
| 31 | extern "C" void gui_print(const char *fmt, ...) |
| 32 | { |
| 33 | char buf[512]; // We're going to limit a single request to 512 bytes |
| 34 | |
| 35 | va_list ap; |
| 36 | va_start(ap, fmt); |
| 37 | vsnprintf(buf, 512, fmt, ap); |
| 38 | va_end(ap); |
| 39 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 40 | fputs(buf, stdout); |
| 41 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | char *start, *next; |
| 43 | |
| 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 | |
| 49 | for (start = next = buf; *next != '\0'; next++) |
| 50 | { |
| 51 | if (*next == '\n') |
| 52 | { |
| 53 | *next = '\0'; |
| 54 | next++; |
| 55 | |
| 56 | std::string line = start; |
| 57 | gConsole.push_back(line); |
| 58 | start = next; |
| 59 | |
| 60 | // Handle the normal \n\0 case |
| 61 | if (*next == '\0') |
| 62 | return; |
| 63 | } |
| 64 | } |
| 65 | std::string line = start; |
| 66 | gConsole.push_back(line); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | extern "C" void gui_print_overwrite(const char *fmt, ...) |
| 71 | { |
| 72 | char buf[512]; // We're going to limit a single request to 512 bytes |
| 73 | |
| 74 | va_list ap; |
| 75 | va_start(ap, fmt); |
| 76 | vsnprintf(buf, 512, fmt, ap); |
| 77 | va_end(ap); |
| 78 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 79 | fputs(buf, stdout); |
| 80 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 81 | // Pop the last line, and we can continue |
| 82 | if (!gConsole.empty()) gConsole.pop_back(); |
| 83 | |
| 84 | 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; |
| 95 | |
| 96 | // Handle the normal \n\0 case |
| 97 | if (*next == '\0') |
| 98 | return; |
| 99 | } |
| 100 | } |
| 101 | std::string line = start; |
| 102 | gConsole.push_back(line); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | GUIConsole::GUIConsole(xml_node<>* node) |
| 107 | { |
| 108 | xml_attribute<>* attr; |
| 109 | xml_node<>* child; |
| 110 | |
| 111 | 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; |
| 121 | |
| 122 | mRenderX = 0; mRenderY = 0; mRenderW = gr_fb_width(); mRenderH = gr_fb_height(); |
| 123 | |
| 124 | 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 | } |
| 138 | |
| 139 | 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 | } |
| 161 | |
| 162 | // Load the placement |
| 163 | LoadPlacement(node->first_node("placement"), &mConsoleX, &mConsoleY, &mConsoleW, &mConsoleH); |
| 164 | |
| 165 | child = node->first_node("slideout"); |
| 166 | if (child) |
| 167 | { |
| 168 | mSlideout = 1; |
| 169 | LoadPlacement(child, &mSlideoutX, &mSlideoutY); |
| 170 | |
| 171 | attr = child->first_attribute("resource"); |
| 172 | if (attr) mSlideoutImage = PageManager::FindResource(attr->value()); |
| 173 | |
| 174 | if (mSlideoutImage && mSlideoutImage->GetResource()) |
| 175 | { |
| 176 | mSlideoutW = gr_get_width(mSlideoutImage->GetResource()); |
| 177 | mSlideoutH = gr_get_height(mSlideoutImage->GetResource()); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | gr_getFontDetails(mFont, &mFontHeight, NULL); |
| 183 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 184 | SetRenderPos(mConsoleX, mConsoleY); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | int GUIConsole::RenderSlideout(void) |
| 189 | { |
| 190 | if (!mSlideoutImage || !mSlideoutImage->GetResource()) return -1; |
| 191 | |
| 192 | gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int GUIConsole::RenderConsole(void) |
| 197 | { |
| 198 | void* fontResource = NULL; |
| 199 | if (mFont) fontResource = mFont->GetResource(); |
| 200 | |
| 201 | // We fill the background |
| 202 | gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255); |
| 203 | gr_fill(mConsoleX, mConsoleY, mConsoleW, mConsoleH); |
| 204 | |
| 205 | gr_color(mScrollColor.red, mScrollColor.green, mScrollColor.blue, mScrollColor.alpha); |
| 206 | gr_fill(mConsoleX + (mConsoleW * 9 / 10), mConsoleY, (mConsoleW / 10), mConsoleH); |
| 207 | |
| 208 | // Render the lines |
| 209 | gr_color(mForegroundColor.red, mForegroundColor.green, mForegroundColor.blue, mForegroundColor.alpha); |
| 210 | |
| 211 | // Don't try to continue to render without data |
| 212 | mLastCount = gConsole.size(); |
| 213 | if (mLastCount == 0) return (mSlideout ? RenderSlideout() : 0); |
| 214 | |
| 215 | // Find the start point |
| 216 | int start; |
| 217 | int curLine = mCurrentLine; // Thread-safing (Another thread updates this value) |
| 218 | if (curLine == -1) |
| 219 | { |
| 220 | start = mLastCount - mMaxRows; |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | if (curLine > (int) mLastCount) curLine = (int) mLastCount; |
| 225 | if ((int) mMaxRows > curLine) curLine = (int) mMaxRows; |
| 226 | start = curLine - mMaxRows; |
| 227 | } |
| 228 | |
| 229 | unsigned int line; |
| 230 | for (line = 0; line < mMaxRows; line++) |
| 231 | { |
| 232 | if ((start + (int) line) >= 0 && (start + (int) line) < (int) mLastCount) |
| 233 | { |
| 234 | gr_textExW(mConsoleX, mStartY + (line * mFontHeight), gConsole[start + line].c_str(), fontResource, mConsoleW + mConsoleX); |
| 235 | } |
| 236 | } |
| 237 | return (mSlideout ? RenderSlideout() : 0); |
| 238 | } |
| 239 | |
| 240 | int GUIConsole::Render(void) |
| 241 | { |
| 242 | if (mSlideout && mSlideoutState == hidden) |
| 243 | { |
| 244 | return RenderSlideout(); |
| 245 | } |
| 246 | return RenderConsole(); |
| 247 | } |
| 248 | |
| 249 | int GUIConsole::Update(void) |
| 250 | { |
| 251 | if (mSlideout && mSlideoutState != visible) |
| 252 | { |
| 253 | if (mSlideoutState == hidden) |
| 254 | return 0; |
| 255 | |
| 256 | if (mSlideoutState == request_hide) |
| 257 | mSlideoutState = hidden; |
| 258 | |
| 259 | if (mSlideoutState == request_show) |
| 260 | mSlideoutState = visible; |
| 261 | |
| 262 | // Any time we activate the slider, we reset the position |
| 263 | mCurrentLine = -1; |
| 264 | return 2; |
| 265 | } |
| 266 | |
| 267 | if (mCurrentLine == -1 && mLastCount != gConsole.size()) |
| 268 | { |
| 269 | // We can use Render, and return for just a flip |
| 270 | Render(); |
| 271 | return 2; |
| 272 | } |
| 273 | else if (mLastTouchY >= 0) |
| 274 | { |
| 275 | // They're still touching, so re-render |
| 276 | Render(); |
gordon1337 | 1035509 | 2013-06-08 14:15:32 +0200 | [diff] [blame] | 277 | mLastTouchY = -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 278 | return 2; |
| 279 | } |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | int GUIConsole::SetRenderPos(int x, int y, int w, int h) |
| 284 | { |
| 285 | // Adjust the stub position accordingly |
| 286 | mSlideoutX += (x - mConsoleX); |
| 287 | mSlideoutY += (y - mConsoleY); |
| 288 | |
| 289 | mConsoleX = x; |
| 290 | mConsoleY = y; |
| 291 | if (w || h) |
| 292 | { |
| 293 | mConsoleW = w; |
| 294 | mConsoleH = h; |
| 295 | } |
| 296 | |
| 297 | // Calculate the max rows |
| 298 | mMaxRows = mConsoleH / mFontHeight; |
| 299 | |
| 300 | // Adjust so we always fit to bottom |
| 301 | mStartY = mConsoleY + (mConsoleH % mFontHeight); |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | // IsInRegion - Checks if the request is handled by this object |
| 306 | // Return 0 if this object handles the request, 1 if not |
| 307 | int GUIConsole::IsInRegion(int x, int y) |
| 308 | { |
| 309 | if (mSlideout) |
| 310 | { |
| 311 | // Check if they tapped the slideout button |
| 312 | if (x >= mSlideoutX && x <= mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH) |
| 313 | return 1; |
| 314 | |
| 315 | // If we're only rendering the slideout, bail now |
| 316 | if (mSlideoutState == hidden) |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | return (x < mConsoleX || x > mConsoleX + mConsoleW || y < mConsoleY || y > mConsoleY + mConsoleH) ? 0 : 1; |
| 321 | } |
| 322 | |
| 323 | // NotifyTouch - Notify of a touch event |
| 324 | // Return 0 on success, >0 to ignore remainder of touch, and <0 on error |
| 325 | int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 326 | { |
| 327 | if (mSlideout && mSlideoutState == hidden) |
| 328 | { |
| 329 | if (state == TOUCH_START) |
| 330 | { |
| 331 | mSlideoutState = request_show; |
| 332 | return 1; |
| 333 | } |
| 334 | } |
| 335 | else if (mSlideout && mSlideoutState == visible) |
| 336 | { |
| 337 | // Are we sliding it back in? |
| 338 | if (state == TOUCH_START && x > mSlideoutX && x < (mSlideoutX + mSlideoutW) && y > mSlideoutY && y < (mSlideoutY + mSlideoutH)) |
| 339 | { |
| 340 | mSlideoutState = request_hide; |
| 341 | return 1; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // If we don't have enough lines to scroll, throw this away. |
| 346 | if (mLastCount < mMaxRows) return 1; |
| 347 | |
| 348 | // We are scrolling!!! |
| 349 | switch (state) |
| 350 | { |
| 351 | case TOUCH_START: |
| 352 | mLastTouchX = x; |
| 353 | mLastTouchY = y; |
| 354 | if ((x - mConsoleX) > ((9 * mConsoleW) / 10)) |
| 355 | mSlideMultiplier = 10; |
| 356 | else |
| 357 | mSlideMultiplier = 1; |
| 358 | break; |
| 359 | |
| 360 | case TOUCH_DRAG: |
| 361 | // This handles tapping |
| 362 | if (x == mLastTouchX && y == mLastTouchY) break; |
| 363 | mLastTouchX = -1; |
| 364 | |
| 365 | if (y > mLastTouchY + 5) |
| 366 | { |
| 367 | mLastTouchY = y; |
| 368 | if (mCurrentLine == -1) |
Dees_Troy | 293d727 | 2012-09-26 10:43:46 -0400 | [diff] [blame] | 369 | mCurrentLine = mLastCount - 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 370 | else if (mCurrentLine > mSlideMultiplier) |
| 371 | mCurrentLine -= mSlideMultiplier; |
| 372 | else |
| 373 | mCurrentLine = mMaxRows; |
| 374 | |
| 375 | if (mCurrentLine < (int) mMaxRows) |
| 376 | mCurrentLine = mMaxRows; |
| 377 | } |
| 378 | else if (y < mLastTouchY - 5) |
| 379 | { |
| 380 | mLastTouchY = y; |
| 381 | if (mCurrentLine >= 0) |
| 382 | { |
| 383 | mCurrentLine += mSlideMultiplier; |
| 384 | if (mCurrentLine >= (int) mLastCount) |
| 385 | mCurrentLine = -1; |
| 386 | } |
| 387 | } |
| 388 | break; |
| 389 | |
| 390 | case TOUCH_RELEASE: |
| 391 | // On a tap, we jump to the tail |
| 392 | if (mLastTouchX >= 0) |
| 393 | mCurrentLine = -1; |
| 394 | |
| 395 | mLastTouchY = -1; |
| 396 | case TOUCH_REPEAT: |
| 397 | case TOUCH_HOLD: |
| 398 | break; |
| 399 | } |
| 400 | return 0; |
| 401 | } |
| 402 | |