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" |
| 35 | #include "../minuitwrp/minui.h" |
| 36 | } |
| 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); |
| 428 | if (ch == 13) |
| 429 | ch = 10; |
| 430 | initPty(); // reinit just in case it died before |
| 431 | // encode the char as UTF-8 and send it to the pty |
| 432 | std::string c; |
| 433 | utf8add(c, (uint32_t)ch); |
| 434 | pty.write(c.c_str(), c.size()); |
| 435 | return true; |
| 436 | } |
| 437 | |
| 438 | bool inputKey(int key) |
| 439 | { |
| 440 | debug_printf("inputKey: %d\n", key); |
| 441 | switch (key) |
| 442 | { |
| 443 | case KEY_UP: pty.write("\e[A"); break; |
| 444 | case KEY_DOWN: pty.write("\e[B"); break; |
| 445 | case KEY_RIGHT: pty.write("\e[C"); break; |
| 446 | case KEY_LEFT: pty.write("\e[D"); break; |
| 447 | case KEY_HOME: pty.write("\eOH"); break; |
| 448 | case KEY_END: pty.write("\eOF"); break; |
| 449 | case KEY_INSERT: pty.write("\e[2~"); break; |
| 450 | case KEY_DELETE: pty.write("\e[3~"); break; |
| 451 | case KEY_PAGEUP: pty.write("\e[5~"); break; |
| 452 | case KEY_PAGEDOWN: pty.write("\e[6~"); break; |
| 453 | // TODO: other keys |
| 454 | default: |
| 455 | return false; |
| 456 | } |
| 457 | return true; |
| 458 | } |
| 459 | |
| 460 | size_t getLinesCount() const { return lines.size(); } |
| 461 | const Line& getLine(size_t n) { if (unpackedY == n) packLine(); return lines[n]; } |
| 462 | int getCursorX() const { return cursorX; } |
| 463 | int getCursorY() const { return cursorY; } |
| 464 | int getUpdateCounter() const { return updateCounter; } |
| 465 | |
| 466 | void setX(int x) |
| 467 | { |
| 468 | x = min(width, max(x, 0)); |
| 469 | cursorX = x; |
| 470 | ++updateCounter; |
| 471 | } |
| 472 | |
| 473 | void setY(int y) |
| 474 | { |
| 475 | //y = min(height, max(y, 0)); |
| 476 | y = max(y, 0); |
| 477 | cursorY = y; |
| 478 | while (lines.size() <= (size_t) y) |
| 479 | lines.push_back(Line()); |
| 480 | ++updateCounter; |
| 481 | } |
| 482 | |
| 483 | void up(int n = 1) { setY(cursorY - n); } |
| 484 | void down(int n = 1) { setY(cursorY + n); } |
| 485 | void left(int n = 1) { setX(cursorX - n); } |
| 486 | void right(int n = 1) { setX(cursorX + n); } |
| 487 | |
| 488 | private: |
| 489 | void packLine() |
| 490 | { |
| 491 | std::string& s = lines[unpackedY].text; |
| 492 | s.clear(); |
| 493 | for (size_t i = 0; i < unpackedLine.cells.size(); ++i) { |
| 494 | Cell& c = unpackedLine.cells[i]; |
| 495 | utf8add(s, c.cp); |
| 496 | // later: if attributes changed, add attributes |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | void unpackLine(size_t y) |
| 501 | { |
| 502 | uint32_t u8state = 0, u8cp = 0; |
| 503 | std::string& s = lines[y].text; |
| 504 | unpackedLine.cells.clear(); |
| 505 | for(size_t i = 0; i < s.size(); ++i) { |
| 506 | uint32_t rc = utf8decode(&u8state, &u8cp, (unsigned char)s[i]); |
| 507 | if (rc == UTF8_ACCEPT) |
| 508 | unpackedLine.cells.push_back(Cell(u8cp)); |
| 509 | } |
| 510 | if (unpackedLine.cells.size() < (size_t)width) |
| 511 | unpackedLine.cells.resize(width); |
| 512 | unpackedY = y; |
| 513 | } |
| 514 | |
| 515 | void ensureUnpacked(size_t y) |
| 516 | { |
| 517 | if (unpackedY != y) |
| 518 | { |
| 519 | packLine(); |
| 520 | unpackLine(y); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | void processC0(char ch) |
| 525 | { |
| 526 | switch (ch) |
| 527 | { |
| 528 | case 7: // BEL |
| 529 | DataManager::Vibrate("tw_button_vibrate"); |
| 530 | break; |
| 531 | case 8: // BS |
| 532 | left(); |
| 533 | break; |
| 534 | case 9: // HT |
| 535 | // TODO: this might be totally wrong |
| 536 | right(); |
| 537 | while (cursorX % 8 != 0 && cursorX < width) |
| 538 | right(); |
| 539 | break; |
| 540 | case 10: // LF |
| 541 | case 11: // VT |
| 542 | case 12: // FF |
| 543 | down(); |
| 544 | break; |
| 545 | case 13: // CR |
| 546 | setX(0); |
| 547 | break; |
| 548 | case 24: // CAN |
| 549 | case 26: // SUB |
| 550 | state = kStateGround; |
| 551 | ctlseq.clear(); |
| 552 | break; |
| 553 | case 27: // ESC |
| 554 | state = kStateEsc; |
| 555 | ctlseq.clear(); |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | void processCodePoint(CodePoint cp) |
| 561 | { |
| 562 | ++updateCounter; |
| 563 | debug_printf("codepoint: %u\n", cp); |
| 564 | if (cp == 0x9b) // CSI |
| 565 | { |
| 566 | state = kStateCsi; |
| 567 | ctlseq.clear(); |
| 568 | return; |
| 569 | } |
| 570 | switch (state) |
| 571 | { |
| 572 | case kStateGround: |
| 573 | processChar(cp); |
| 574 | break; |
| 575 | case kStateEsc: |
| 576 | processEsc(cp); |
| 577 | break; |
| 578 | case kStateCsi: |
| 579 | processControlSequence(cp); |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | void processChar(CodePoint cp) |
| 585 | { |
| 586 | ensureUnpacked(cursorY); |
| 587 | // extend unpackedLine if needed, write ch into cell |
| 588 | if (unpackedLine.cells.size() <= (size_t)cursorX) |
| 589 | unpackedLine.cells.resize(cursorX+1); |
| 590 | unpackedLine.cells[cursorX].cp = cp; |
| 591 | |
| 592 | right(); |
| 593 | if (cursorX >= width) |
| 594 | { |
| 595 | // TODO: configurable line wrapping |
| 596 | // TODO: don't go down immediately but only on next char? |
| 597 | down(); |
| 598 | setX(0); |
| 599 | } |
| 600 | // TODO: update all GUI objects that display this terminal engine |
| 601 | } |
| 602 | |
| 603 | void processEsc(CodePoint cp) |
| 604 | { |
| 605 | switch (cp) { |
| 606 | case 'c': // TODO: Reset |
| 607 | break; |
| 608 | case 'D': // Line feed |
| 609 | down(); |
| 610 | break; |
| 611 | case 'E': // Newline |
| 612 | setX(0); |
| 613 | down(); |
| 614 | break; |
| 615 | case '[': // CSI |
| 616 | state = kStateCsi; |
| 617 | ctlseq.clear(); |
| 618 | break; |
| 619 | case ']': // TODO: OSC state |
| 620 | default: |
| 621 | state = kStateGround; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | void processControlSequence(CodePoint cp) |
| 626 | { |
| 627 | if (cp >= 0x40 && cp <= 0x7e) { |
| 628 | ctlseq += cp; |
| 629 | execControlSequence(ctlseq); |
| 630 | ctlseq.clear(); |
| 631 | state = kStateGround; |
| 632 | return; |
| 633 | } |
| 634 | if (isdigit(cp) || cp == ';' /* || (ch >= 0x3c && ch <= 0x3f) */) { |
| 635 | ctlseq += cp; |
| 636 | // state = kStateCsiParam; |
| 637 | return; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | static int parseArg(std::string& s, int defaultvalue) |
| 642 | { |
| 643 | if (s.empty() || !isdigit(s[0])) |
| 644 | return defaultvalue; |
| 645 | int value = atoi(s.c_str()); |
| 646 | size_t pos = s.find(';'); |
| 647 | s.erase(0, pos != std::string::npos ? pos+1 : std::string::npos); |
| 648 | return value; |
| 649 | } |
| 650 | |
| 651 | void execControlSequence(std::string ctlseq) |
| 652 | { |
| 653 | // assert(!ctlseq.empty()); |
| 654 | if (ctlseq == "6n") { |
| 655 | // CPR - cursor position report |
| 656 | char answer[20]; |
| 657 | sprintf(answer, "\e[%d;%dR", cursorY, cursorX); |
| 658 | pty.write(answer, strlen(answer)); |
| 659 | return; |
| 660 | } |
| 661 | char f = *ctlseq.rbegin(); |
| 662 | // if (f == '?') ... private mode |
| 663 | switch (f) |
| 664 | { |
| 665 | // case '@': // ICH - insert character |
| 666 | case 'A': // CUU - cursor up |
| 667 | up(parseArg(ctlseq, 1)); |
| 668 | break; |
| 669 | case 'B': // CUD - cursor down |
| 670 | case 'e': // VPR - line position forward |
| 671 | down(parseArg(ctlseq, 1)); |
| 672 | break; |
| 673 | case 'C': // CUF - cursor right |
| 674 | case 'a': // HPR - character position forward |
| 675 | right(parseArg(ctlseq, 1)); |
| 676 | break; |
| 677 | case 'D': // CUB - cursor left |
| 678 | left(parseArg(ctlseq, 1)); |
| 679 | break; |
| 680 | case 'E': // CNL - cursor next line |
| 681 | down(parseArg(ctlseq, 1)); |
| 682 | setX(0); |
| 683 | break; |
| 684 | case 'F': // CPL - cursor preceding line |
| 685 | up(parseArg(ctlseq, 1)); |
| 686 | setX(0); |
| 687 | break; |
| 688 | case 'G': // CHA - cursor character absolute |
| 689 | setX(parseArg(ctlseq, 1)-1); |
| 690 | break; |
| 691 | case 'H': // CUP - cursor position |
| 692 | // TODO: consider scrollback area |
| 693 | setY(parseArg(ctlseq, 1)-1); |
| 694 | setX(parseArg(ctlseq, 1)-1); |
| 695 | break; |
| 696 | case 'J': // ED - erase in page |
| 697 | { |
| 698 | int param = parseArg(ctlseq, 0); |
| 699 | ensureUnpacked(cursorY); |
| 700 | switch (param) { |
| 701 | default: |
| 702 | case 0: |
| 703 | unpackedLine.eraseFrom(cursorX); |
| 704 | if (lines.size() > (size_t)cursorY+1) |
| 705 | lines.erase(lines.begin() + cursorY+1, lines.end()); |
| 706 | break; |
| 707 | case 1: |
| 708 | unpackedLine.eraseTo(cursorX); |
| 709 | if (cursorY > 0) { |
| 710 | lines.erase(lines.begin(), lines.begin() + cursorY-1); |
| 711 | cursorY = 0; |
| 712 | } |
| 713 | break; |
| 714 | case 2: // clear |
| 715 | case 3: // clear incl scrollback |
| 716 | clear(); |
| 717 | break; |
| 718 | } |
| 719 | } |
| 720 | break; |
| 721 | case 'K': // EL - erase in line |
| 722 | { |
| 723 | int param = parseArg(ctlseq, 0); |
| 724 | ensureUnpacked(cursorY); |
| 725 | switch (param) { |
| 726 | default: |
| 727 | case 0: |
| 728 | unpackedLine.eraseFrom(cursorX); |
| 729 | break; |
| 730 | case 1: |
| 731 | unpackedLine.eraseTo(cursorX); |
| 732 | break; |
| 733 | case 2: |
| 734 | unpackedLine.cells.clear(); |
| 735 | break; |
| 736 | } |
| 737 | } |
| 738 | break; |
| 739 | // case 'L': // IL - insert line |
| 740 | |
| 741 | default: |
| 742 | debug_printf("unknown ctlseq: '%s'\n", ctlseq.c_str()); |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | private: |
| 748 | int cursorX, cursorY; // 0-based, char based. TODO: decide how to handle scrollback |
| 749 | int width, height; // window size in chars |
| 750 | std::vector<Line> lines; // the text buffer |
| 751 | UnpackedLine unpackedLine; // current line for editing |
| 752 | size_t unpackedY; // number of current line |
| 753 | int updateCounter; // changes whenever terminal could require redraw |
| 754 | |
| 755 | Pseudoterminal pty; |
| 756 | enum { kStateGround, kStateEsc, kStateCsi } state; |
| 757 | |
| 758 | // for accumulating a full UTF-8 character from individual bytes |
| 759 | uint32_t utf8state; |
| 760 | uint32_t utf8codepoint; |
| 761 | |
| 762 | // for accumulating a control sequence after receiving CSI |
| 763 | std::string ctlseq; |
| 764 | }; |
| 765 | |
| 766 | // The one and only terminal engine for now |
| 767 | TerminalEngine gEngine; |
| 768 | |
| 769 | void terminal_pty_read() |
| 770 | { |
| 771 | gEngine.readPty(); |
| 772 | } |
| 773 | |
| 774 | |
| 775 | GUITerminal::GUITerminal(xml_node<>* node) : GUIScrollList(node) |
| 776 | { |
| 777 | allowSelection = false; // terminal doesn't support list item selections |
| 778 | lastCondition = false; |
| 779 | |
| 780 | if (!node) { |
| 781 | mRenderX = 0; mRenderY = 0; mRenderW = gr_fb_width(); mRenderH = gr_fb_height(); |
| 782 | } |
| 783 | |
| 784 | engine = &gEngine; |
| 785 | updateCounter = 0; |
| 786 | } |
| 787 | |
| 788 | int GUITerminal::Update(void) |
| 789 | { |
| 790 | if(!isConditionTrue()) { |
| 791 | lastCondition = false; |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | if (lastCondition == false) { |
| 796 | lastCondition = true; |
| 797 | // we're becoming visible, so we might need to resize the terminal content |
| 798 | InitAndResize(); |
| 799 | } |
| 800 | |
| 801 | if (updateCounter != engine->getUpdateCounter()) { |
| 802 | // try to keep the cursor in view |
| 803 | SetVisibleListLocation(engine->getCursorY()); |
| 804 | updateCounter = engine->getUpdateCounter(); |
| 805 | } |
| 806 | |
| 807 | GUIScrollList::Update(); |
| 808 | |
| 809 | if (mUpdate) { |
| 810 | mUpdate = 0; |
| 811 | if (Render() == 0) |
| 812 | return 2; |
| 813 | } |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | // NotifyTouch - Notify of a touch event |
| 818 | // Return 0 on success, >0 to ignore remainder of touch, and <0 on error |
| 819 | int GUITerminal::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 820 | { |
| 821 | if(!isConditionTrue()) |
| 822 | return -1; |
| 823 | |
| 824 | // TODO: grab focus correctly |
| 825 | // TODO: fix focus handling in PageManager and GUIInput |
| 826 | SetInputFocus(1); |
| 827 | debug_printf("Terminal: SetInputFocus\n"); |
| 828 | return GUIScrollList::NotifyTouch(state, x, y); |
| 829 | // TODO later: allow cursor positioning by touch (simulate mouse click?) |
| 830 | // http://stackoverflow.com/questions/5966903/how-to-get-mousemove-and-mouseclick-in-bash |
| 831 | // will likely not work with Busybox anyway |
| 832 | } |
| 833 | |
| 834 | int GUITerminal::NotifyKey(int key, bool down) |
| 835 | { |
that | d4725ca | 2016-01-19 00:15:21 +0100 | [diff] [blame] | 836 | if (!HasInputFocus) |
| 837 | return 1; |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 838 | if (down) |
| 839 | if (engine->inputKey(key)) |
| 840 | mUpdate = 1; |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | // character input |
| 845 | int GUITerminal::NotifyCharInput(int ch) |
| 846 | { |
| 847 | if (engine->inputChar(ch)) |
| 848 | mUpdate = 1; |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | size_t GUITerminal::GetItemCount() |
| 853 | { |
| 854 | return engine->getLinesCount(); |
| 855 | } |
| 856 | |
| 857 | void GUITerminal::RenderItem(size_t itemindex, int yPos, bool selected) |
| 858 | { |
| 859 | const TerminalEngine::Line& line = engine->getLine(itemindex); |
| 860 | |
| 861 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha); |
| 862 | // later: handle attributes here |
| 863 | |
| 864 | // render text |
| 865 | const char* text = line.text.c_str(); |
| 866 | gr_textEx_scaleW(mRenderX, yPos, text, mFont->GetResource(), mRenderW, TOP_LEFT, 0); |
| 867 | |
| 868 | if (itemindex == (size_t) engine->getCursorY()) { |
| 869 | // render cursor |
| 870 | int cursorX = engine->getCursorX(); |
| 871 | std::string leftOfCursor = line.substr(0, cursorX); |
| 872 | int x = gr_measureEx(leftOfCursor.c_str(), mFont->GetResource()); |
| 873 | // note that this single character can be a UTF-8 sequence |
| 874 | std::string atCursor = (size_t)cursorX < line.length() ? line.substr(cursorX, 1) : " "; |
| 875 | int w = gr_measureEx(atCursor.c_str(), mFont->GetResource()); |
| 876 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha); |
| 877 | gr_fill(mRenderX + x, yPos, w, actualItemHeight); |
| 878 | gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, mBackgroundColor.alpha); |
| 879 | gr_textEx_scaleW(mRenderX + x, yPos, atCursor.c_str(), mFont->GetResource(), mRenderW, TOP_LEFT, 0); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | void GUITerminal::NotifySelect(size_t item_selected) |
| 884 | { |
| 885 | // do nothing - terminal ignores selections |
| 886 | } |
| 887 | |
| 888 | void GUITerminal::InitAndResize() |
| 889 | { |
| 890 | // make sure the shell is started |
| 891 | engine->initPty(); |
| 892 | // send window resize |
| 893 | int charWidth = gr_measureEx("N", mFont->GetResource()); |
| 894 | engine->setSize(mRenderW / charWidth, GetDisplayItemCount(), mRenderW, mRenderH); |
| 895 | } |
| 896 | |
| 897 | void GUITerminal::SetPageFocus(int inFocus) |
| 898 | { |
| 899 | if (inFocus && isConditionTrue()) |
| 900 | InitAndResize(); |
| 901 | } |