Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1 | /* |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 2 | Copyright 2012 - 2020 TeamWin |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | // console.cpp - GUIConsole object |
| 20 | |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <fcntl.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 26 | #include <time.h> |
| 27 | #include <unistd.h> |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 28 | #include <pthread.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 29 | |
| 30 | #include <string> |
| 31 | |
| 32 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 33 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | } |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame] | 35 | #include "minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | |
| 37 | #include "rapidxml.hpp" |
| 38 | #include "objects.hpp" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 39 | #include "gui.hpp" |
| 40 | #include "twmsg.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 42 | #define GUI_CONSOLE_BUFFER_SIZE 512 |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 44 | static pthread_mutex_t console_lock; |
| 45 | static size_t last_message_count = 0; |
| 46 | static std::vector<Message> gMessages; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 47 | |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 48 | static std::vector<std::string> gConsole; |
| 49 | static std::vector<std::string> gConsoleColor; |
| 50 | static FILE* ors_file = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 52 | struct InitMutex |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | { |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 54 | InitMutex() { pthread_mutex_init(&console_lock, NULL); } |
| 55 | } initMutex; |
| 56 | |
| 57 | static void internal_gui_print(const char *color, char *buf) |
| 58 | { |
| 59 | // make sure to flush any outstanding messages first to preserve order of outputs |
| 60 | GUIConsole::Translate_Now(); |
| 61 | |
| 62 | fputs(buf, stdout); |
| 63 | if (ors_file) { |
| 64 | fprintf(ors_file, "%s", buf); |
| 65 | fflush(ors_file); |
| 66 | } |
| 67 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 68 | char *start, *next; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 69 | |
| 70 | if (buf[0] == '\n' && strlen(buf) < 2) { |
| 71 | // This prevents the double lines bug seen in the console during zip installs |
| 72 | return; |
| 73 | } |
| 74 | |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 75 | pthread_mutex_lock(&console_lock); |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 76 | for (start = next = buf; *next != '\0';) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 77 | { |
| 78 | if (*next == '\n') |
| 79 | { |
| 80 | *next = '\0'; |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 81 | gConsole.push_back(start); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 82 | gConsoleColor.push_back(color); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 83 | |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 84 | start = ++next; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 85 | } |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 86 | else |
| 87 | ++next; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 88 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 89 | |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 90 | // The text after last \n (or whole string if there is no \n) |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 91 | if (*start) { |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 92 | gConsole.push_back(start); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 93 | gConsoleColor.push_back(color); |
| 94 | } |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 95 | pthread_mutex_unlock(&console_lock); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | extern "C" void gui_print(const char *fmt, ...) |
| 99 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 100 | char buf[GUI_CONSOLE_BUFFER_SIZE]; // We're going to limit a single request to 512 bytes |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 101 | |
| 102 | va_list ap; |
| 103 | va_start(ap, fmt); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 104 | vsnprintf(buf, GUI_CONSOLE_BUFFER_SIZE, fmt, ap); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 105 | va_end(ap); |
| 106 | |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 107 | internal_gui_print("normal", buf); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | extern "C" void gui_print_color(const char *color, const char *fmt, ...) |
| 111 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 112 | char buf[GUI_CONSOLE_BUFFER_SIZE]; // We're going to limit a single request to 512 bytes |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 113 | |
| 114 | va_list ap; |
| 115 | va_start(ap, fmt); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 116 | vsnprintf(buf, GUI_CONSOLE_BUFFER_SIZE, fmt, ap); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 117 | va_end(ap); |
| 118 | |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 119 | internal_gui_print(color, buf); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 122 | extern "C" void gui_set_FILE(FILE* f) |
| 123 | { |
| 124 | ors_file = f; |
| 125 | } |
| 126 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 127 | void gui_msg(const char* text) |
| 128 | { |
| 129 | if (text) { |
| 130 | Message msg = Msg(text); |
| 131 | gui_msg(msg); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void gui_warn(const char* text) |
| 136 | { |
| 137 | if (text) { |
| 138 | Message msg = Msg(msg::kWarning, text); |
| 139 | gui_msg(msg); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void gui_err(const char* text) |
| 144 | { |
| 145 | if (text) { |
| 146 | Message msg = Msg(msg::kError, text); |
| 147 | gui_msg(msg); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void gui_highlight(const char* text) |
| 152 | { |
| 153 | if (text) { |
| 154 | Message msg = Msg(msg::kHighlight, text); |
| 155 | gui_msg(msg); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void gui_msg(Message msg) |
| 160 | { |
| 161 | std::string output = msg; |
| 162 | output += "\n"; |
| 163 | fputs(output.c_str(), stdout); |
| 164 | if (ors_file) { |
| 165 | fprintf(ors_file, "%s", output.c_str()); |
| 166 | fflush(ors_file); |
| 167 | } |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 168 | pthread_mutex_lock(&console_lock); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 169 | gMessages.push_back(msg); |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 170 | pthread_mutex_unlock(&console_lock); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 171 | } |
| 172 | |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 173 | void GUIConsole::Translate_Now() |
| 174 | { |
| 175 | pthread_mutex_lock(&console_lock); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 176 | size_t message_count = gMessages.size(); |
| 177 | if (message_count <= last_message_count) |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 178 | { |
| 179 | pthread_mutex_unlock(&console_lock); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 180 | return; |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 181 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 182 | |
| 183 | for (size_t m = last_message_count; m < message_count; m++) { |
| 184 | std::string message = gMessages[m]; |
| 185 | std::string color = "normal"; |
| 186 | if (gMessages[m].GetKind() == msg::kError) |
| 187 | color = "error"; |
| 188 | else if (gMessages[m].GetKind() == msg::kHighlight) |
| 189 | color = "highlight"; |
| 190 | else if (gMessages[m].GetKind() == msg::kWarning) |
| 191 | color = "warning"; |
| 192 | gConsole.push_back(message); |
| 193 | gConsoleColor.push_back(color); |
| 194 | } |
| 195 | last_message_count = message_count; |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 196 | pthread_mutex_unlock(&console_lock); |
| 197 | } |
| 198 | |
| 199 | void GUIConsole::Clear_For_Retranslation() |
| 200 | { |
| 201 | pthread_mutex_lock(&console_lock); |
| 202 | last_message_count = 0; |
| 203 | gConsole.clear(); |
| 204 | gConsoleColor.clear(); |
| 205 | pthread_mutex_unlock(&console_lock); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 206 | } |
| 207 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 208 | GUIConsole::GUIConsole(xml_node<>* node) : GUIScrollList(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 209 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 210 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 211 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 212 | mLastCount = 0; |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 213 | scrollToEnd = true; |
| 214 | mSlideoutX = mSlideoutY = mSlideoutW = mSlideoutH = 0; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 215 | mSlideout = 0; |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 216 | mSlideoutState = visible; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 217 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 218 | allowSelection = false; // console doesn't support list item selections |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 219 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 220 | if (!node) |
| 221 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 222 | mRenderX = 0; |
| 223 | mRenderY = 0; |
| 224 | mRenderW = gr_fb_width(); |
| 225 | mRenderH = gr_fb_height(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 226 | } |
| 227 | else |
| 228 | { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 229 | child = FindNode(node, "color"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 230 | if (child) |
| 231 | { |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 232 | mFontColor = LoadAttrColor(child, "foreground", mFontColor); |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 233 | mBackgroundColor = LoadAttrColor(child, "background", mBackgroundColor); |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 234 | //mScrollColor = LoadAttrColor(child, "scroll", mScrollColor); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 235 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 236 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 237 | child = FindNode(node, "slideout"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 238 | if (child) |
| 239 | { |
| 240 | mSlideout = 1; |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 241 | mSlideoutState = hidden; |
Ethan Yonker | 591b920 | 2015-03-11 11:17:15 -0500 | [diff] [blame] | 242 | LoadPlacement(child, &mSlideoutX, &mSlideoutY, &mSlideoutW, &mSlideoutH, &mPlacement); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 243 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 244 | mSlideoutImage = LoadAttrImage(child, "resource"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 245 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 246 | if (mSlideoutImage && mSlideoutImage->GetResource()) |
| 247 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 248 | mSlideoutW = mSlideoutImage->GetWidth(); |
| 249 | mSlideoutH = mSlideoutImage->GetHeight(); |
Ethan Yonker | 591b920 | 2015-03-11 11:17:15 -0500 | [diff] [blame] | 250 | if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY) { |
| 251 | mSlideoutX = mSlideoutX - (mSlideoutW / 2); |
| 252 | if (mPlacement == CENTER) { |
| 253 | mSlideoutY = mSlideoutY - (mSlideoutH / 2); |
| 254 | } |
| 255 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | int GUIConsole::RenderSlideout(void) |
| 262 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 263 | if (!mSlideoutImage || !mSlideoutImage->GetResource()) |
| 264 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 265 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 266 | gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY); |
| 267 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 268 | } |
| 269 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 270 | int GUIConsole::RenderConsole(void) |
| 271 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 272 | Translate_Now(); |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 273 | pthread_mutex_lock(&console_lock); |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 274 | AddLines(&gConsole, &gConsoleColor, &mLastCount, &rConsole, &rConsoleColor); |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 275 | pthread_mutex_unlock(&console_lock); |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 276 | GUIScrollList::Render(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 277 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 278 | // if last line is fully visible, keep tracking the last line when new lines are added |
| 279 | int bottom_offset = GetDisplayRemainder() - actualItemHeight; |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 280 | bool isAtBottom = firstDisplayedItem == (int)GetItemCount() - GetDisplayItemCount() - (bottom_offset != 0) && y_offset == bottom_offset; |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 281 | if (isAtBottom) |
| 282 | scrollToEnd = true; |
| 283 | #if 0 |
| 284 | // debug - show if we are tracking the last line |
| 285 | if (scrollToEnd) { |
| 286 | gr_color(0,255,0,255); |
| 287 | gr_fill(mRenderX+mRenderW-5, mRenderY+mRenderH-5, 5, 5); |
| 288 | } else { |
| 289 | gr_color(255,0,0,255); |
| 290 | gr_fill(mRenderX+mRenderW-5, mRenderY+mRenderH-5, 5, 5); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 291 | } |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 292 | #endif |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 293 | return (mSlideout ? RenderSlideout() : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | int GUIConsole::Render(void) |
| 297 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 298 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 299 | return 0; |
| 300 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 301 | if (mSlideout && mSlideoutState == hidden) |
| 302 | return RenderSlideout(); |
| 303 | |
| 304 | return RenderConsole(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | int GUIConsole::Update(void) |
| 308 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 309 | if (mSlideout && mSlideoutState != visible) |
| 310 | { |
| 311 | if (mSlideoutState == hidden) |
| 312 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 313 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 314 | if (mSlideoutState == request_hide) |
| 315 | mSlideoutState = hidden; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 316 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 317 | if (mSlideoutState == request_show) |
| 318 | mSlideoutState = visible; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 319 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 320 | // Any time we activate the console, we reset the position |
| 321 | SetVisibleListLocation(rConsole.size() - 1); |
| 322 | mUpdate = 1; |
| 323 | scrollToEnd = true; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 324 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 325 | |
that | a9dd9f0 | 2017-02-23 23:08:56 +0100 | [diff] [blame] | 326 | pthread_mutex_lock(&console_lock); |
| 327 | bool addedNewText = AddLines(&gConsole, &gConsoleColor, &mLastCount, &rConsole, &rConsoleColor); |
| 328 | pthread_mutex_unlock(&console_lock); |
| 329 | if (addedNewText) { |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 330 | // someone added new text |
| 331 | // at least the scrollbar must be updated, even if the new lines are currently not visible |
| 332 | mUpdate = 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 333 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 334 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 335 | if (scrollToEnd) { |
| 336 | // keep the last line in view |
| 337 | SetVisibleListLocation(rConsole.size() - 1); |
| 338 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 339 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 340 | GUIScrollList::Update(); |
| 341 | |
| 342 | if (mUpdate) { |
| 343 | mUpdate = 0; |
| 344 | if (Render() == 0) |
| 345 | return 2; |
| 346 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 347 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // IsInRegion - Checks if the request is handled by this object |
that | f8194e2 | 2015-01-29 01:05:01 +0100 | [diff] [blame] | 351 | // Return 1 if this object handles the request, 0 if not |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 352 | int GUIConsole::IsInRegion(int x, int y) |
| 353 | { |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 354 | if (mSlideout) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 355 | // Check if they tapped the slideout button |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 356 | if (x >= mSlideoutX && x < mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 357 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 358 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 359 | // If we're only rendering the slideout, bail now |
| 360 | if (mSlideoutState == hidden) |
| 361 | return 0; |
| 362 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 363 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 364 | return GUIScrollList::IsInRegion(x, y); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // NotifyTouch - Notify of a touch event |
| 368 | // Return 0 on success, >0 to ignore remainder of touch, and <0 on error |
| 369 | int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 370 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 371 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 372 | return -1; |
| 373 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 374 | if (mSlideout && x >= mSlideoutX && x < mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH) { |
| 375 | if (state == TOUCH_START) { |
| 376 | if (mSlideoutState == hidden) |
| 377 | mSlideoutState = request_show; |
| 378 | else if (mSlideoutState == visible) |
| 379 | mSlideoutState = request_hide; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 380 | } |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 381 | return 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 382 | } |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 383 | scrollToEnd = false; |
| 384 | return GUIScrollList::NotifyTouch(state, x, y); |
| 385 | } |
| 386 | |
| 387 | size_t GUIConsole::GetItemCount() |
| 388 | { |
| 389 | return rConsole.size(); |
| 390 | } |
| 391 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 392 | void GUIConsole::RenderItem(size_t itemindex, int yPos, bool selected __unused) |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 393 | { |
| 394 | // Set the color for the font |
| 395 | if (rConsoleColor[itemindex] == "normal") { |
| 396 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha); |
| 397 | } else { |
| 398 | COLOR FontColor; |
| 399 | std::string color = rConsoleColor[itemindex]; |
| 400 | ConvertStrToColor(color, &FontColor); |
| 401 | FontColor.alpha = 255; |
| 402 | gr_color(FontColor.red, FontColor.green, FontColor.blue, FontColor.alpha); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 403 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 404 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 405 | // render text |
| 406 | const char* text = rConsole[itemindex].c_str(); |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 407 | gr_textEx_scaleW(mRenderX, yPos, text, mFont->GetResource(), mRenderW, TOP_LEFT, 0); |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 408 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 409 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 410 | void GUIConsole::NotifySelect(size_t item_selected __unused) |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 411 | { |
| 412 | // do nothing - console ignores selections |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 413 | } |