that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2016 _that/TeamWin |
| 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 | |
| 19 | // terminal.cpp - GUITerminal object |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <unistd.h> |
| 26 | #include <termio.h> |
| 27 | |
| 28 | #include <string> |
| 29 | #include <cctype> |
| 30 | #include <linux/input.h> |
that | a17f175 | 2016-01-11 01:07:28 +0100 | [diff] [blame] | 31 | #include <sys/wait.h> |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 32 | |
| 33 | extern "C" { |
| 34 | #include "../twcommon.h" |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 35 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 36 | #include "../minuitwrp/minui.h" |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 37 | |
| 38 | #include "rapidxml.hpp" |
| 39 | #include "objects.hpp" |
| 40 | |
| 41 | #if 0 |
| 42 | #define debug_printf printf |
| 43 | #else |
| 44 | #define debug_printf(...) |
| 45 | #endif |
| 46 | |
| 47 | extern int g_pty_fd; // in gui.cpp where the select is |
| 48 | |
| 49 | /* |
| 50 | Pseudoterminal handler. |
| 51 | */ |
| 52 | class Pseudoterminal |
| 53 | { |
| 54 | public: |
| 55 | Pseudoterminal() : fdMaster(0), pid(0) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | bool started() const { return pid > 0; } |
| 60 | |
| 61 | bool start() |
| 62 | { |
| 63 | fdMaster = getpt(); |
| 64 | if (fdMaster < 0) { |
| 65 | LOGERR("Error %d on getpt()\n", errno); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | if (unlockpt(fdMaster) != 0) { |
| 70 | LOGERR("Error %d on unlockpt()\n", errno); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | pid = fork(); |
| 75 | if (pid < 0) { |
| 76 | LOGERR("fork failed for pty, error %d\n", errno); |
| 77 | close(fdMaster); |
| 78 | pid = 0; |
| 79 | return false; |
| 80 | } |
| 81 | else if (pid) { |
| 82 | // child started, now someone needs to periodically read from fdMaster |
| 83 | // and write it to the terminal |
| 84 | // this currently works through gui.cpp calling terminal_pty_read below |
| 85 | g_pty_fd = fdMaster; |
| 86 | return true; |
| 87 | } |
| 88 | else { |
| 89 | int fdSlave = open(ptsname(fdMaster), O_RDWR); |
| 90 | close(fdMaster); |
| 91 | runSlave(fdSlave); |
| 92 | } |
| 93 | // we can't get here |
| 94 | LOGERR("impossible error in pty\n"); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | void runSlave(int fdSlave) |
| 99 | { |
| 100 | dup2(fdSlave, 0); // PTY becomes standard input (0) |
| 101 | dup2(fdSlave, 1); // PTY becomes standard output (1) |
| 102 | dup2(fdSlave, 2); // PTY becomes standard error (2) |
| 103 | |
| 104 | // Now the original file descriptor is useless |
| 105 | close(fdSlave); |
| 106 | |
| 107 | // Make the current process a new session leader |
| 108 | if (setsid() == (pid_t)-1) |
| 109 | LOGERR("setsid failed: %d\n", errno); |
| 110 | |
| 111 | // As the child is a session leader, set the controlling terminal to be the slave side of the PTY |
| 112 | // (Mandatory for programs like the shell to make them manage correctly their outputs) |
| 113 | ioctl(0, TIOCSCTTY, 1); |
| 114 | |
| 115 | execl("/sbin/sh", "sh", NULL); |
| 116 | _exit(127); |
| 117 | } |
| 118 | |
| 119 | int read(char* buffer, size_t size) |
| 120 | { |
| 121 | if (!started()) { |
| 122 | LOGERR("someone tried to read from pty, but it was not started\n"); |
| 123 | return -1; |
| 124 | } |
| 125 | int rc = ::read(fdMaster, buffer, size); |
| 126 | debug_printf("pty read: %d bytes\n", rc); |
| 127 | if (rc < 0) { |
that | a17f175 | 2016-01-11 01:07:28 +0100 | [diff] [blame] | 128 | // assume child has died (usual errno when shell exits seems to be EIO == 5) |
| 129 | if (errno != EIO) |
| 130 | LOGERR("pty read failed: %d\n", errno); |
| 131 | stop(); |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 132 | } |
| 133 | return rc; |
| 134 | } |
| 135 | |
| 136 | int write(const char* buffer, size_t size) |
| 137 | { |
| 138 | if (!started()) { |
| 139 | LOGERR("someone tried to write to pty, but it was not started\n"); |
| 140 | return -1; |
| 141 | } |
| 142 | int rc = ::write(fdMaster, buffer, size); |
| 143 | debug_printf("pty write: %d bytes -> %d\n", size, rc); |
| 144 | if (rc < 0) { |
that | a17f175 | 2016-01-11 01:07:28 +0100 | [diff] [blame] | 145 | LOGERR("pty write failed: %d\n", errno); |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 146 | // assume child has died |
that | a17f175 | 2016-01-11 01:07:28 +0100 | [diff] [blame] | 147 | stop(); |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 148 | } |
| 149 | return rc; |
| 150 | } |
| 151 | |
| 152 | template<size_t n> |
| 153 | inline int write(const char (&literal)[n]) |
| 154 | { |
| 155 | return write(literal, n-1); |
| 156 | } |
| 157 | |
| 158 | void resize(int xChars, int yChars, int w, int h) |
| 159 | { |
| 160 | struct winsize ws; |
| 161 | ws.ws_row = yChars; |
| 162 | ws.ws_col = xChars; |
| 163 | ws.ws_xpixel = w; |
| 164 | ws.ws_ypixel = h; |
| 165 | if (ioctl(fdMaster, TIOCSWINSZ, &ws) < 0) |
| 166 | LOGERR("failed to set window size, error %d\n", errno); |
| 167 | } |
| 168 | |
that | a17f175 | 2016-01-11 01:07:28 +0100 | [diff] [blame] | 169 | void stop() |
| 170 | { |
| 171 | if (!started()) { |
| 172 | LOGERR("someone tried to stop pty, but it was not started\n"); |
| 173 | return; |
| 174 | } |
| 175 | close(fdMaster); |
| 176 | g_pty_fd = fdMaster = -1; |
| 177 | int status; |
| 178 | waitpid(pid, &status, WNOHANG); // avoid zombies but don't hang if the child is still alive and we got here due to some error |
| 179 | pid = 0; |
| 180 | } |
| 181 | |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 182 | private: |
| 183 | int fdMaster; |
that | a17f175 | 2016-01-11 01:07:28 +0100 | [diff] [blame] | 184 | pid_t pid; |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | // UTF-8 decoder |
| 188 | // Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de> |
| 189 | // See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. |
| 190 | |
| 191 | const uint32_t UTF8_ACCEPT = 0; |
| 192 | const uint32_t UTF8_REJECT = 1; |
| 193 | |
| 194 | static const uint8_t utf8d[] = { |
| 195 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00..1f |
| 196 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 20..3f |
| 197 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 40..5f |
| 198 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 60..7f |
| 199 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f |
| 200 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf |
| 201 | 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df |
| 202 | 0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef |
| 203 | 0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff |
| 204 | 0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0 |
| 205 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2 |
| 206 | 1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4 |
| 207 | 1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6 |
| 208 | 1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8 |
| 209 | }; |
| 210 | |
| 211 | uint32_t inline utf8decode(uint32_t* state, uint32_t* codep, uint32_t byte) |
| 212 | { |
| 213 | uint32_t type = utf8d[byte]; |
| 214 | |
| 215 | *codep = (*state != UTF8_ACCEPT) ? |
| 216 | (byte & 0x3fu) | (*codep << 6) : |
| 217 | (0xff >> type) & (byte); |
| 218 | |
| 219 | *state = utf8d[256 + *state*16 + type]; |
| 220 | return *state; |
| 221 | } |
| 222 | // end of UTF-8 decoder |
| 223 | |
| 224 | // Append a UTF-8 codepoint to string s |
| 225 | size_t utf8add(std::string& s, uint32_t cp) |
| 226 | { |
| 227 | if (cp < 0x7f) { |
| 228 | s += cp; |
| 229 | return 1; |
| 230 | } |
| 231 | else if (cp < 0x7ff) { |
| 232 | s += (0xc0 | (cp >> 6)); |
| 233 | s += (0x80 | (cp & 0x3f)); |
| 234 | return 2; |
| 235 | } |
| 236 | else if (cp < 0xffff) { |
| 237 | s += (0xe0 | (cp >> 12)); |
| 238 | s += (0x80 | ((cp >> 6) & 0x3f)); |
| 239 | s += (0x80 | (cp & 0x3f)); |
| 240 | return 3; |
| 241 | } |
| 242 | else if (cp < 0x1fffff) { |
| 243 | s += (0xf0 | (cp >> 18)); |
| 244 | s += (0x80 | ((cp >> 12) & 0x3f)); |
| 245 | s += (0x80 | ((cp >> 6) & 0x3f)); |
| 246 | s += (0x80 | (cp & 0x3f)); |
| 247 | return 4; |
| 248 | } |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | TerminalEngine is the terminal back-end, dealing with the text buffer and attributes |
| 254 | and with communicating with the pty. |
| 255 | It does not care about visual things like rendering, fonts, windows etc. |
| 256 | The idea is that 0 to n GUITerminal instances (e.g. on different pages) can connect |
| 257 | to one TerminalEngine to interact with the terminal, and that the TerminalEngine |
| 258 | survives things like page changes or even theme reloads. |
| 259 | */ |
| 260 | class TerminalEngine |
| 261 | { |
| 262 | public: |
| 263 | #if 0 // later |
| 264 | struct Attributes |
| 265 | { |
| 266 | COLOR fgcolor; // TODO: what about palette? |
| 267 | COLOR bgcolor; |
| 268 | // could add bold, underline, blink, etc. |
| 269 | }; |
| 270 | |
| 271 | struct AttributeRange |
| 272 | { |
| 273 | size_t start; // start position inside text (in bytes) |
| 274 | Attributes a; |
| 275 | }; |
| 276 | #endif |
| 277 | typedef uint32_t CodePoint; // Unicode code point |
| 278 | |
| 279 | // A line of text, optimized for rendering and storage in the buffer |
| 280 | struct Line |
| 281 | { |
| 282 | std::string text; // in UTF-8 format |
| 283 | // std::vector<AttributeRange> attrs; |
| 284 | Line() {} |
| 285 | size_t utf8forward(size_t start) const |
| 286 | { |
| 287 | if (start >= text.size()) |
| 288 | return start; |
| 289 | uint32_t u8state = 0, u8cp = 0; |
| 290 | size_t i = start; |
| 291 | uint32_t rc; |
| 292 | do { |
| 293 | rc = utf8decode(&u8state, &u8cp, (unsigned char)text[i]); |
| 294 | ++i; |
| 295 | } while (rc != UTF8_ACCEPT && rc != UTF8_REJECT && i < text.size()); |
| 296 | return i; |
| 297 | } |
| 298 | |
| 299 | std::string substr(size_t start, size_t n) const |
| 300 | { |
| 301 | size_t i = 0; |
| 302 | for (; start && i < text.size(); i = utf8forward(i)) |
| 303 | --start; |
| 304 | size_t s = i; |
| 305 | for (; n && i < text.size(); i = utf8forward(i)) |
| 306 | --n; |
| 307 | return text.substr(s, i - s); |
| 308 | } |
| 309 | size_t length() const |
| 310 | { |
| 311 | size_t n = 0; |
| 312 | for (size_t i = 0; i < text.size(); i = utf8forward(i)) |
| 313 | ++n; |
| 314 | return n; |
| 315 | } |
| 316 | }; |
| 317 | |
| 318 | // A single character cell with a Unicode code point |
| 319 | struct Cell |
| 320 | { |
| 321 | Cell() : cp(' ') {} |
| 322 | Cell(CodePoint cp) : cp(cp) {} |
| 323 | CodePoint cp; |
| 324 | // Attributes a; |
| 325 | }; |
| 326 | |
| 327 | // A line of text, optimized for editing single characters |
| 328 | struct UnpackedLine |
| 329 | { |
| 330 | std::vector<Cell> cells; |
| 331 | void eraseFrom(size_t x) |
| 332 | { |
| 333 | if (cells.size() > x) |
| 334 | cells.erase(cells.begin() + x, cells.end()); |
| 335 | } |
| 336 | |
| 337 | void eraseTo(size_t x) |
| 338 | { |
| 339 | if (x > 0) |
| 340 | cells.erase(cells.begin(), cells.begin() + x); |
| 341 | } |
| 342 | }; |
| 343 | |
| 344 | TerminalEngine() |
| 345 | { |
| 346 | // the default size will be overwritten by the GUI window when the size is known |
| 347 | width = 40; |
| 348 | height = 10; |
| 349 | |
| 350 | clear(); |
| 351 | updateCounter = 0; |
| 352 | state = kStateGround; |
| 353 | utf8state = utf8codepoint = 0; |
| 354 | } |
| 355 | |
| 356 | void setSize(int xChars, int yChars, int w, int h) |
| 357 | { |
| 358 | width = xChars; |
| 359 | height = yChars; |
| 360 | if (pty.started()) |
| 361 | pty.resize(width, height, w, h); |
| 362 | debug_printf("setSize: %d*%d chars, %d*%d pixels\n", xChars, yChars, w, h); |
| 363 | } |
| 364 | |
| 365 | void initPty() |
| 366 | { |
| 367 | if (!pty.started()) |
| 368 | { |
| 369 | pty.start(); |
| 370 | pty.resize(width, height, 0, 0); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void readPty() |
| 375 | { |
| 376 | char buffer[1024]; |
| 377 | int rc = pty.read(buffer, sizeof(buffer)); |
| 378 | debug_printf("readPty: %d bytes\n", rc); |
| 379 | if (rc < 0) |
| 380 | output("\r\nChild process exited.\r\n"); // TODO: maybe exit terminal here |
| 381 | else |
| 382 | for (int i = 0; i < rc; ++i) |
| 383 | output(buffer[i]); |
| 384 | } |
| 385 | |
| 386 | void clear() |
| 387 | { |
| 388 | cursorX = cursorY = 0; |
| 389 | lines.clear(); |
| 390 | setY(0); |
| 391 | unpackLine(0); |
| 392 | ++updateCounter; |
| 393 | } |
| 394 | |
| 395 | void output(const char *buf) |
| 396 | { |
| 397 | for (const char* p = buf; *p; ++p) |
| 398 | output(*p); |
| 399 | } |
| 400 | |
| 401 | void output(const char ch) |
| 402 | { |
| 403 | char debug[2]; debug[0] = ch; debug[1] = 0; |
| 404 | debug_printf("output: %d %s\n", (int)ch, (ch >= ' ' && ch < 127) ? debug : ch == 27 ? "esc" : ""); |
| 405 | if (ch < 32) { |
| 406 | // always process control chars, even after incomplete UTF-8 fragments |
| 407 | processC0(ch); |
| 408 | if (utf8state != UTF8_ACCEPT) |
| 409 | { |
| 410 | debug_printf("Terminal: incomplete UTF-8 fragment before control char ignored, codepoint=%u ch=%d\n", utf8codepoint, (int)ch); |
| 411 | utf8state = UTF8_ACCEPT; |
| 412 | } |
| 413 | return; |
| 414 | } |
| 415 | uint32_t rc = utf8decode(&utf8state, &utf8codepoint, (unsigned char)ch); |
| 416 | if (rc == UTF8_ACCEPT) |
| 417 | processCodePoint(utf8codepoint); |
| 418 | else if (rc == UTF8_REJECT) { |
| 419 | debug_printf("Terminal: invalid UTF-8 sequence ignored, codepoint=%u ch=%d\n", utf8codepoint, (int)ch); |
| 420 | utf8state = UTF8_ACCEPT; |
| 421 | } |
| 422 | // else we need to read more bytes to assemble a codepoint |
| 423 | } |
| 424 | |
| 425 | bool inputChar(int ch) |
| 426 | { |
| 427 | debug_printf("inputChar: %d\n", ch); |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 428 | initPty(); // reinit just in case it died before |
| 429 | // encode the char as UTF-8 and send it to the pty |
| 430 | std::string c; |
| 431 | utf8add(c, (uint32_t)ch); |
| 432 | pty.write(c.c_str(), c.size()); |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | bool inputKey(int key) |
| 437 | { |
| 438 | debug_printf("inputKey: %d\n", key); |
| 439 | switch (key) |
| 440 | { |
| 441 | case KEY_UP: pty.write("\e[A"); break; |
| 442 | case KEY_DOWN: pty.write("\e[B"); break; |
| 443 | case KEY_RIGHT: pty.write("\e[C"); break; |
| 444 | case KEY_LEFT: pty.write("\e[D"); break; |
| 445 | case KEY_HOME: pty.write("\eOH"); break; |
| 446 | case KEY_END: pty.write("\eOF"); break; |
| 447 | case KEY_INSERT: pty.write("\e[2~"); break; |
| 448 | case KEY_DELETE: pty.write("\e[3~"); break; |
| 449 | case KEY_PAGEUP: pty.write("\e[5~"); break; |
| 450 | case KEY_PAGEDOWN: pty.write("\e[6~"); break; |
| 451 | // TODO: other keys |
| 452 | default: |
| 453 | return false; |
| 454 | } |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | size_t getLinesCount() const { return lines.size(); } |
| 459 | const Line& getLine(size_t n) { if (unpackedY == n) packLine(); return lines[n]; } |
| 460 | int getCursorX() const { return cursorX; } |
| 461 | int getCursorY() const { return cursorY; } |
| 462 | int getUpdateCounter() const { return updateCounter; } |
| 463 | |
| 464 | void setX(int x) |
| 465 | { |
| 466 | x = min(width, max(x, 0)); |
| 467 | cursorX = x; |
| 468 | ++updateCounter; |
| 469 | } |
| 470 | |
| 471 | void setY(int y) |
| 472 | { |
| 473 | //y = min(height, max(y, 0)); |
| 474 | y = max(y, 0); |
| 475 | cursorY = y; |
| 476 | while (lines.size() <= (size_t) y) |
| 477 | lines.push_back(Line()); |
| 478 | ++updateCounter; |
| 479 | } |
| 480 | |
| 481 | void up(int n = 1) { setY(cursorY - n); } |
| 482 | void down(int n = 1) { setY(cursorY + n); } |
| 483 | void left(int n = 1) { setX(cursorX - n); } |
| 484 | void right(int n = 1) { setX(cursorX + n); } |
| 485 | |
| 486 | private: |
| 487 | void packLine() |
| 488 | { |
| 489 | std::string& s = lines[unpackedY].text; |
| 490 | s.clear(); |
| 491 | for (size_t i = 0; i < unpackedLine.cells.size(); ++i) { |
| 492 | Cell& c = unpackedLine.cells[i]; |
| 493 | utf8add(s, c.cp); |
| 494 | // later: if attributes changed, add attributes |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void unpackLine(size_t y) |
| 499 | { |
| 500 | uint32_t u8state = 0, u8cp = 0; |
| 501 | std::string& s = lines[y].text; |
| 502 | unpackedLine.cells.clear(); |
| 503 | for(size_t i = 0; i < s.size(); ++i) { |
| 504 | uint32_t rc = utf8decode(&u8state, &u8cp, (unsigned char)s[i]); |
| 505 | if (rc == UTF8_ACCEPT) |
| 506 | unpackedLine.cells.push_back(Cell(u8cp)); |
| 507 | } |
| 508 | if (unpackedLine.cells.size() < (size_t)width) |
| 509 | unpackedLine.cells.resize(width); |
| 510 | unpackedY = y; |
| 511 | } |
| 512 | |
| 513 | void ensureUnpacked(size_t y) |
| 514 | { |
| 515 | if (unpackedY != y) |
| 516 | { |
| 517 | packLine(); |
| 518 | unpackLine(y); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | void processC0(char ch) |
| 523 | { |
| 524 | switch (ch) |
| 525 | { |
| 526 | case 7: // BEL |
| 527 | DataManager::Vibrate("tw_button_vibrate"); |
| 528 | break; |
| 529 | case 8: // BS |
| 530 | left(); |
| 531 | break; |
| 532 | case 9: // HT |
| 533 | // TODO: this might be totally wrong |
| 534 | right(); |
| 535 | while (cursorX % 8 != 0 && cursorX < width) |
| 536 | right(); |
| 537 | break; |
| 538 | case 10: // LF |
| 539 | case 11: // VT |
| 540 | case 12: // FF |
| 541 | down(); |
| 542 | break; |
| 543 | case 13: // CR |
| 544 | setX(0); |
| 545 | break; |
| 546 | case 24: // CAN |
| 547 | case 26: // SUB |
| 548 | state = kStateGround; |
| 549 | ctlseq.clear(); |
| 550 | break; |
| 551 | case 27: // ESC |
| 552 | state = kStateEsc; |
| 553 | ctlseq.clear(); |
| 554 | break; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | void processCodePoint(CodePoint cp) |
| 559 | { |
| 560 | ++updateCounter; |
| 561 | debug_printf("codepoint: %u\n", cp); |
| 562 | if (cp == 0x9b) // CSI |
| 563 | { |
| 564 | state = kStateCsi; |
| 565 | ctlseq.clear(); |
| 566 | return; |
| 567 | } |
| 568 | switch (state) |
| 569 | { |
| 570 | case kStateGround: |
| 571 | processChar(cp); |
| 572 | break; |
| 573 | case kStateEsc: |
| 574 | processEsc(cp); |
| 575 | break; |
| 576 | case kStateCsi: |
| 577 | processControlSequence(cp); |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | void processChar(CodePoint cp) |
| 583 | { |
| 584 | ensureUnpacked(cursorY); |
| 585 | // extend unpackedLine if needed, write ch into cell |
| 586 | if (unpackedLine.cells.size() <= (size_t)cursorX) |
| 587 | unpackedLine.cells.resize(cursorX+1); |
| 588 | unpackedLine.cells[cursorX].cp = cp; |
| 589 | |
| 590 | right(); |
| 591 | if (cursorX >= width) |
| 592 | { |
| 593 | // TODO: configurable line wrapping |
| 594 | // TODO: don't go down immediately but only on next char? |
| 595 | down(); |
| 596 | setX(0); |
| 597 | } |
| 598 | // TODO: update all GUI objects that display this terminal engine |
| 599 | } |
| 600 | |
| 601 | void processEsc(CodePoint cp) |
| 602 | { |
| 603 | switch (cp) { |
| 604 | case 'c': // TODO: Reset |
| 605 | break; |
| 606 | case 'D': // Line feed |
| 607 | down(); |
| 608 | break; |
| 609 | case 'E': // Newline |
| 610 | setX(0); |
| 611 | down(); |
| 612 | break; |
| 613 | case '[': // CSI |
| 614 | state = kStateCsi; |
| 615 | ctlseq.clear(); |
| 616 | break; |
| 617 | case ']': // TODO: OSC state |
| 618 | default: |
| 619 | state = kStateGround; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | void processControlSequence(CodePoint cp) |
| 624 | { |
| 625 | if (cp >= 0x40 && cp <= 0x7e) { |
| 626 | ctlseq += cp; |
| 627 | execControlSequence(ctlseq); |
| 628 | ctlseq.clear(); |
| 629 | state = kStateGround; |
| 630 | return; |
| 631 | } |
| 632 | if (isdigit(cp) || cp == ';' /* || (ch >= 0x3c && ch <= 0x3f) */) { |
| 633 | ctlseq += cp; |
| 634 | // state = kStateCsiParam; |
| 635 | return; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | static int parseArg(std::string& s, int defaultvalue) |
| 640 | { |
| 641 | if (s.empty() || !isdigit(s[0])) |
| 642 | return defaultvalue; |
| 643 | int value = atoi(s.c_str()); |
| 644 | size_t pos = s.find(';'); |
| 645 | s.erase(0, pos != std::string::npos ? pos+1 : std::string::npos); |
| 646 | return value; |
| 647 | } |
| 648 | |
| 649 | void execControlSequence(std::string ctlseq) |
| 650 | { |
| 651 | // assert(!ctlseq.empty()); |
| 652 | if (ctlseq == "6n") { |
| 653 | // CPR - cursor position report |
| 654 | char answer[20]; |
| 655 | sprintf(answer, "\e[%d;%dR", cursorY, cursorX); |
| 656 | pty.write(answer, strlen(answer)); |
| 657 | return; |
| 658 | } |
| 659 | char f = *ctlseq.rbegin(); |
| 660 | // if (f == '?') ... private mode |
| 661 | switch (f) |
| 662 | { |
| 663 | // case '@': // ICH - insert character |
| 664 | case 'A': // CUU - cursor up |
| 665 | up(parseArg(ctlseq, 1)); |
| 666 | break; |
| 667 | case 'B': // CUD - cursor down |
| 668 | case 'e': // VPR - line position forward |
| 669 | down(parseArg(ctlseq, 1)); |
| 670 | break; |
| 671 | case 'C': // CUF - cursor right |
| 672 | case 'a': // HPR - character position forward |
| 673 | right(parseArg(ctlseq, 1)); |
| 674 | break; |
| 675 | case 'D': // CUB - cursor left |
| 676 | left(parseArg(ctlseq, 1)); |
| 677 | break; |
| 678 | case 'E': // CNL - cursor next line |
| 679 | down(parseArg(ctlseq, 1)); |
| 680 | setX(0); |
| 681 | break; |
| 682 | case 'F': // CPL - cursor preceding line |
| 683 | up(parseArg(ctlseq, 1)); |
| 684 | setX(0); |
| 685 | break; |
| 686 | case 'G': // CHA - cursor character absolute |
| 687 | setX(parseArg(ctlseq, 1)-1); |
| 688 | break; |
| 689 | case 'H': // CUP - cursor position |
| 690 | // TODO: consider scrollback area |
| 691 | setY(parseArg(ctlseq, 1)-1); |
| 692 | setX(parseArg(ctlseq, 1)-1); |
| 693 | break; |
| 694 | case 'J': // ED - erase in page |
| 695 | { |
| 696 | int param = parseArg(ctlseq, 0); |
| 697 | ensureUnpacked(cursorY); |
| 698 | switch (param) { |
| 699 | default: |
| 700 | case 0: |
| 701 | unpackedLine.eraseFrom(cursorX); |
| 702 | if (lines.size() > (size_t)cursorY+1) |
| 703 | lines.erase(lines.begin() + cursorY+1, lines.end()); |
| 704 | break; |
| 705 | case 1: |
| 706 | unpackedLine.eraseTo(cursorX); |
| 707 | if (cursorY > 0) { |
| 708 | lines.erase(lines.begin(), lines.begin() + cursorY-1); |
| 709 | cursorY = 0; |
| 710 | } |
| 711 | break; |
| 712 | case 2: // clear |
| 713 | case 3: // clear incl scrollback |
| 714 | clear(); |
| 715 | break; |
| 716 | } |
| 717 | } |
| 718 | break; |
| 719 | case 'K': // EL - erase in line |
| 720 | { |
| 721 | int param = parseArg(ctlseq, 0); |
| 722 | ensureUnpacked(cursorY); |
| 723 | switch (param) { |
| 724 | default: |
| 725 | case 0: |
| 726 | unpackedLine.eraseFrom(cursorX); |
| 727 | break; |
| 728 | case 1: |
| 729 | unpackedLine.eraseTo(cursorX); |
| 730 | break; |
| 731 | case 2: |
| 732 | unpackedLine.cells.clear(); |
| 733 | break; |
| 734 | } |
| 735 | } |
| 736 | break; |
| 737 | // case 'L': // IL - insert line |
| 738 | |
| 739 | default: |
| 740 | debug_printf("unknown ctlseq: '%s'\n", ctlseq.c_str()); |
| 741 | break; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | private: |
| 746 | int cursorX, cursorY; // 0-based, char based. TODO: decide how to handle scrollback |
| 747 | int width, height; // window size in chars |
| 748 | std::vector<Line> lines; // the text buffer |
| 749 | UnpackedLine unpackedLine; // current line for editing |
| 750 | size_t unpackedY; // number of current line |
| 751 | int updateCounter; // changes whenever terminal could require redraw |
| 752 | |
| 753 | Pseudoterminal pty; |
| 754 | enum { kStateGround, kStateEsc, kStateCsi } state; |
| 755 | |
| 756 | // for accumulating a full UTF-8 character from individual bytes |
| 757 | uint32_t utf8state; |
| 758 | uint32_t utf8codepoint; |
| 759 | |
| 760 | // for accumulating a control sequence after receiving CSI |
| 761 | std::string ctlseq; |
| 762 | }; |
| 763 | |
| 764 | // The one and only terminal engine for now |
| 765 | TerminalEngine gEngine; |
| 766 | |
| 767 | void terminal_pty_read() |
| 768 | { |
| 769 | gEngine.readPty(); |
| 770 | } |
| 771 | |
| 772 | |
| 773 | GUITerminal::GUITerminal(xml_node<>* node) : GUIScrollList(node) |
| 774 | { |
| 775 | allowSelection = false; // terminal doesn't support list item selections |
| 776 | lastCondition = false; |
| 777 | |
| 778 | if (!node) { |
| 779 | mRenderX = 0; mRenderY = 0; mRenderW = gr_fb_width(); mRenderH = gr_fb_height(); |
| 780 | } |
| 781 | |
| 782 | engine = &gEngine; |
| 783 | updateCounter = 0; |
| 784 | } |
| 785 | |
| 786 | int GUITerminal::Update(void) |
| 787 | { |
| 788 | if(!isConditionTrue()) { |
| 789 | lastCondition = false; |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | if (lastCondition == false) { |
| 794 | lastCondition = true; |
| 795 | // we're becoming visible, so we might need to resize the terminal content |
| 796 | InitAndResize(); |
| 797 | } |
| 798 | |
| 799 | if (updateCounter != engine->getUpdateCounter()) { |
| 800 | // try to keep the cursor in view |
| 801 | SetVisibleListLocation(engine->getCursorY()); |
| 802 | updateCounter = engine->getUpdateCounter(); |
| 803 | } |
| 804 | |
| 805 | GUIScrollList::Update(); |
| 806 | |
| 807 | if (mUpdate) { |
| 808 | mUpdate = 0; |
| 809 | if (Render() == 0) |
| 810 | return 2; |
| 811 | } |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | // NotifyTouch - Notify of a touch event |
| 816 | // Return 0 on success, >0 to ignore remainder of touch, and <0 on error |
| 817 | int GUITerminal::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 818 | { |
| 819 | if(!isConditionTrue()) |
| 820 | return -1; |
| 821 | |
| 822 | // TODO: grab focus correctly |
| 823 | // TODO: fix focus handling in PageManager and GUIInput |
| 824 | SetInputFocus(1); |
| 825 | debug_printf("Terminal: SetInputFocus\n"); |
| 826 | return GUIScrollList::NotifyTouch(state, x, y); |
| 827 | // TODO later: allow cursor positioning by touch (simulate mouse click?) |
| 828 | // http://stackoverflow.com/questions/5966903/how-to-get-mousemove-and-mouseclick-in-bash |
| 829 | // will likely not work with Busybox anyway |
| 830 | } |
| 831 | |
| 832 | int GUITerminal::NotifyKey(int key, bool down) |
| 833 | { |
that | d4725ca | 2016-01-19 00:15:21 +0100 | [diff] [blame] | 834 | if (!HasInputFocus) |
| 835 | return 1; |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 836 | if (down) |
| 837 | if (engine->inputKey(key)) |
| 838 | mUpdate = 1; |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | // character input |
| 843 | int GUITerminal::NotifyCharInput(int ch) |
| 844 | { |
| 845 | if (engine->inputChar(ch)) |
| 846 | mUpdate = 1; |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | size_t GUITerminal::GetItemCount() |
| 851 | { |
| 852 | return engine->getLinesCount(); |
| 853 | } |
| 854 | |
| 855 | void GUITerminal::RenderItem(size_t itemindex, int yPos, bool selected) |
| 856 | { |
| 857 | const TerminalEngine::Line& line = engine->getLine(itemindex); |
| 858 | |
| 859 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha); |
| 860 | // later: handle attributes here |
| 861 | |
| 862 | // render text |
| 863 | const char* text = line.text.c_str(); |
| 864 | gr_textEx_scaleW(mRenderX, yPos, text, mFont->GetResource(), mRenderW, TOP_LEFT, 0); |
| 865 | |
| 866 | if (itemindex == (size_t) engine->getCursorY()) { |
| 867 | // render cursor |
| 868 | int cursorX = engine->getCursorX(); |
| 869 | std::string leftOfCursor = line.substr(0, cursorX); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 870 | int x = gr_ttf_measureEx(leftOfCursor.c_str(), mFont->GetResource()); |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 871 | // note that this single character can be a UTF-8 sequence |
| 872 | std::string atCursor = (size_t)cursorX < line.length() ? line.substr(cursorX, 1) : " "; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 873 | int w = gr_ttf_measureEx(atCursor.c_str(), mFont->GetResource()); |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 874 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha); |
| 875 | gr_fill(mRenderX + x, yPos, w, actualItemHeight); |
| 876 | gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, mBackgroundColor.alpha); |
| 877 | gr_textEx_scaleW(mRenderX + x, yPos, atCursor.c_str(), mFont->GetResource(), mRenderW, TOP_LEFT, 0); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | void GUITerminal::NotifySelect(size_t item_selected) |
| 882 | { |
| 883 | // do nothing - terminal ignores selections |
| 884 | } |
| 885 | |
| 886 | void GUITerminal::InitAndResize() |
| 887 | { |
| 888 | // make sure the shell is started |
| 889 | engine->initPty(); |
| 890 | // send window resize |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 891 | int charWidth = gr_ttf_measureEx("N", mFont->GetResource()); |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 892 | engine->setSize(mRenderW / charWidth, GetDisplayItemCount(), mRenderW, mRenderH); |
| 893 | } |
| 894 | |
| 895 | void GUITerminal::SetPageFocus(int inFocus) |
| 896 | { |
Ethan Yonker | ec0b432 | 2016-03-31 14:04:14 -0500 | [diff] [blame] | 897 | if (inFocus && isConditionTrue()) { |
| 898 | // TODO: grab focus correctly, this hack grabs focus and insists that the terminal be the focus regardless of other elements |
| 899 | // It's highly unlikely that there will be any other visible input elements on the page anyway... |
| 900 | SetInputFocus(1); |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 901 | InitAndResize(); |
Ethan Yonker | ec0b432 | 2016-03-31 14:04:14 -0500 | [diff] [blame] | 902 | } |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 903 | } |