VirtualConsole.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  4. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "VirtualConsole.h"
  9. #include <AK/StdLibExtras.h>
  10. #include <AK/String.h>
  11. #include <Kernel/Arch/x86/CPU.h>
  12. #include <Kernel/Debug.h>
  13. #include <Kernel/Devices/HID/HIDManagement.h>
  14. #include <Kernel/Graphics/GraphicsManagement.h>
  15. #include <Kernel/Heap/kmalloc.h>
  16. #include <Kernel/IO.h>
  17. #include <Kernel/StdLib.h>
  18. #include <Kernel/TTY/ConsoleManagement.h>
  19. #include <LibVT/Color.h>
  20. namespace Kernel {
  21. ConsoleImpl::ConsoleImpl(VirtualConsole& client)
  22. : Terminal(client)
  23. {
  24. }
  25. void ConsoleImpl::invalidate_cursor()
  26. {
  27. }
  28. void ConsoleImpl::clear()
  29. {
  30. m_client.clear();
  31. }
  32. void ConsoleImpl::clear_history()
  33. {
  34. }
  35. void ConsoleImpl::set_size(u16 determined_columns, u16 determined_rows)
  36. {
  37. VERIFY(determined_columns);
  38. VERIFY(determined_rows);
  39. if (determined_columns == columns() && determined_rows == rows())
  40. return;
  41. m_columns = determined_columns;
  42. m_rows = determined_rows;
  43. m_scroll_region_top = 0;
  44. m_scroll_region_bottom = determined_rows - 1;
  45. m_current_state.cursor.clamp(rows() - 1, columns() - 1);
  46. m_normal_saved_state.cursor.clamp(rows() - 1, columns() - 1);
  47. m_alternate_saved_state.cursor.clamp(rows() - 1, columns() - 1);
  48. m_saved_cursor_position.clamp(rows() - 1, columns() - 1);
  49. m_horizontal_tabs.resize(determined_columns);
  50. for (unsigned i = 0; i < determined_columns; ++i)
  51. m_horizontal_tabs[i] = (i % 8) == 0;
  52. // Rightmost column is always last tab on line.
  53. m_horizontal_tabs[determined_columns - 1] = 1;
  54. m_client.terminal_did_resize(m_columns, m_rows);
  55. }
  56. void ConsoleImpl::scroll_up(u16 region_top, u16 region_bottom, size_t count)
  57. {
  58. // NOTE: We have to invalidate the cursor first.
  59. m_client.invalidate_cursor(cursor_row());
  60. m_client.scroll_up(region_top, region_bottom, count);
  61. }
  62. void ConsoleImpl::scroll_down(u16 region_top, u16 region_bottom, size_t count)
  63. {
  64. m_client.invalidate_cursor(cursor_row());
  65. m_client.scroll_down(region_top, region_bottom, count);
  66. }
  67. void ConsoleImpl::put_character_at(unsigned row, unsigned column, u32 ch)
  68. {
  69. m_client.put_character_at(row, column, ch, m_current_state.attribute);
  70. m_last_code_point = ch;
  71. }
  72. void ConsoleImpl::clear_in_line(u16 row, u16 first_column, u16 last_column)
  73. {
  74. m_client.clear_in_line(row, first_column, last_column);
  75. }
  76. void ConsoleImpl::scroll_left(u16 row, u16 column, size_t count)
  77. {
  78. m_client.scroll_left(row, column, count);
  79. }
  80. void ConsoleImpl::scroll_right(u16 row, u16 column, size_t count)
  81. {
  82. m_client.scroll_right(row, column, count);
  83. }
  84. void VirtualConsole::set_graphical(bool graphical)
  85. {
  86. m_graphical = graphical;
  87. }
  88. UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create(size_t index)
  89. {
  90. return adopt_ref(*new VirtualConsole(index));
  91. }
  92. UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create_with_preset_log(size_t index, const CircularQueue<char, 16384>& log)
  93. {
  94. return adopt_ref(*new VirtualConsole(index, log));
  95. }
  96. UNMAP_AFTER_INIT void VirtualConsole::initialize()
  97. {
  98. m_tty_name = String::formatted("/dev/tty{}", m_index);
  99. VERIFY(GraphicsManagement::the().console());
  100. set_size(GraphicsManagement::the().console()->max_column(), GraphicsManagement::the().console()->max_row());
  101. m_console_impl.set_size(GraphicsManagement::the().console()->max_column(), GraphicsManagement::the().console()->max_row());
  102. // Allocate twice of the max row * max column * sizeof(Cell) to ensure we can have some sort of history mechanism...
  103. auto size = GraphicsManagement::the().console()->max_column() * GraphicsManagement::the().console()->max_row() * sizeof(Cell) * 2;
  104. m_cells = MM.allocate_kernel_region(page_round_up(size), "Virtual Console Cells", Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow);
  105. // Add the lines, so we also ensure they will be flushed now
  106. for (size_t row = 0; row < rows(); row++) {
  107. m_lines.append({ true, 0 });
  108. }
  109. clear();
  110. VERIFY(m_cells);
  111. }
  112. void VirtualConsole::refresh_after_resolution_change()
  113. {
  114. auto old_rows_count = rows();
  115. auto old_columns_count = columns();
  116. set_size(GraphicsManagement::the().console()->max_column(), GraphicsManagement::the().console()->max_row());
  117. m_console_impl.set_size(GraphicsManagement::the().console()->max_column(), GraphicsManagement::the().console()->max_row());
  118. // Note: From now on, columns() and rows() are updated with the new settings.
  119. auto size = GraphicsManagement::the().console()->max_column() * GraphicsManagement::the().console()->max_row() * sizeof(Cell) * 2;
  120. auto new_cells = MM.allocate_kernel_region(page_round_up(size), "Virtual Console Cells", Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow);
  121. if (rows() < old_rows_count) {
  122. m_lines.shrink(rows());
  123. } else {
  124. for (size_t row = 0; row < (size_t)(rows() - old_rows_count); row++) {
  125. m_lines.append({ true, 0 });
  126. }
  127. }
  128. // Note: A potential loss of displayed data occur when resolution width shrinks.
  129. if (columns() < old_columns_count) {
  130. for (size_t row = 0; row < rows(); row++) {
  131. auto& line = m_lines[row];
  132. memcpy(new_cells->vaddr().offset((row)*columns() * sizeof(Cell)).as_ptr(), m_cells->vaddr().offset((row) * (old_columns_count) * sizeof(Cell)).as_ptr(), columns() * sizeof(Cell));
  133. line.dirty = true;
  134. }
  135. } else {
  136. // Handle Growth of resolution
  137. for (size_t row = 0; row < rows(); row++) {
  138. auto& line = m_lines[row];
  139. memcpy(new_cells->vaddr().offset((row)*columns() * sizeof(Cell)).as_ptr(), m_cells->vaddr().offset((row) * (old_columns_count) * sizeof(Cell)).as_ptr(), old_columns_count * sizeof(Cell));
  140. line.dirty = true;
  141. }
  142. }
  143. // Update the new cells Region
  144. m_cells = move(new_cells);
  145. m_console_impl.m_need_full_flush = true;
  146. flush_dirty_lines();
  147. }
  148. UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index)
  149. : TTY(4, index)
  150. , m_index(index)
  151. , m_console_impl(*this)
  152. {
  153. initialize();
  154. }
  155. UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index, const CircularQueue<char, 16384>& log)
  156. : TTY(4, index)
  157. , m_index(index)
  158. , m_console_impl(*this)
  159. {
  160. initialize();
  161. // HACK: We have to go through the TTY layer for correct newline handling.
  162. // It would be nice to not have to make all these calls, but we can't get the underlying data pointer
  163. // and head index. If we did that, we could reduce this to at most 2 calls.
  164. for (auto ch : log) {
  165. emit_char(ch);
  166. }
  167. }
  168. UNMAP_AFTER_INIT VirtualConsole::~VirtualConsole()
  169. {
  170. VERIFY_NOT_REACHED();
  171. }
  172. static inline Graphics::Console::Color ansi_color_to_standard_vga_color(VT::Color::ANSIColor color)
  173. {
  174. switch (color) {
  175. case VT::Color::ANSIColor::DefaultBackground:
  176. case VT::Color::ANSIColor::Black:
  177. return Graphics::Console::Color::Black;
  178. case VT::Color::ANSIColor::Red:
  179. return Graphics::Console::Color::Red;
  180. case VT::Color::ANSIColor::Green:
  181. return Graphics::Console::Color::Green;
  182. case VT::Color::ANSIColor::Yellow:
  183. // VGA only has bright yellow, and treats normal yellow as a brownish orange color.
  184. return Graphics::Console::Color::Brown;
  185. case VT::Color::ANSIColor::Blue:
  186. return Graphics::Console::Color::Blue;
  187. case VT::Color::ANSIColor::Magenta:
  188. return Graphics::Console::Color::Magenta;
  189. case VT::Color::ANSIColor::Cyan:
  190. return Graphics::Console::Color::Cyan;
  191. case VT::Color::ANSIColor::DefaultForeground:
  192. case VT::Color::ANSIColor::White:
  193. return Graphics::Console::Color::LightGray;
  194. case VT::Color::ANSIColor::BrightBlack:
  195. return Graphics::Console::Color::DarkGray;
  196. case VT::Color::ANSIColor::BrightRed:
  197. return Graphics::Console::Color::BrightRed;
  198. case VT::Color::ANSIColor::BrightGreen:
  199. return Graphics::Console::Color::BrightGreen;
  200. case VT::Color::ANSIColor::BrightYellow:
  201. return Graphics::Console::Color::Yellow;
  202. case VT::Color::ANSIColor::BrightBlue:
  203. return Graphics::Console::Color::BrightBlue;
  204. case VT::Color::ANSIColor::BrightMagenta:
  205. return Graphics::Console::Color::BrightMagenta;
  206. case VT::Color::ANSIColor::BrightCyan:
  207. return Graphics::Console::Color::BrightCyan;
  208. case VT::Color::ANSIColor::BrightWhite:
  209. return Graphics::Console::Color::White;
  210. }
  211. VERIFY_NOT_REACHED();
  212. }
  213. static inline Graphics::Console::Color terminal_to_standard_color(VT::Color color)
  214. {
  215. switch (color.kind()) {
  216. case VT::Color::Kind::Named:
  217. return ansi_color_to_standard_vga_color(color.as_named());
  218. default:
  219. return Graphics::Console::Color::LightGray;
  220. }
  221. }
  222. void VirtualConsole::on_key_pressed(KeyEvent event)
  223. {
  224. // Ignore keyboard in graphical mode.
  225. if (m_graphical)
  226. return;
  227. if (!event.is_press())
  228. return;
  229. Processor::deferred_call_queue([this, event]() {
  230. m_console_impl.handle_key_press(event.key, event.code_point, event.flags);
  231. });
  232. }
  233. ssize_t VirtualConsole::on_tty_write(const UserOrKernelBuffer& data, ssize_t size)
  234. {
  235. ScopedSpinLock global_lock(ConsoleManagement::the().tty_write_lock());
  236. ScopedSpinLock lock(m_lock);
  237. auto result = data.read_buffered<512>((size_t)size, [&](u8 const* buffer, size_t buffer_bytes) {
  238. for (size_t i = 0; i < buffer_bytes; ++i)
  239. m_console_impl.on_input(buffer[i]);
  240. return buffer_bytes;
  241. });
  242. if (m_active)
  243. flush_dirty_lines();
  244. if (result.is_error())
  245. return result.error();
  246. return (ssize_t)result.value();
  247. }
  248. void VirtualConsole::set_active(bool active)
  249. {
  250. VERIFY(ConsoleManagement::the().m_lock.is_locked());
  251. VERIFY(m_active != active);
  252. m_active = active;
  253. if (active) {
  254. HIDManagement::the().set_client(this);
  255. m_console_impl.m_need_full_flush = true;
  256. flush_dirty_lines();
  257. } else {
  258. HIDManagement::the().set_client(nullptr);
  259. }
  260. }
  261. void VirtualConsole::emit_char(char ch)
  262. {
  263. // Since we are standards-compliant by not moving to column 1 on '\n', we have to add an extra carriage return to
  264. // do newlines properly. The `TTY` layer handles adding it.
  265. echo_with_processing(static_cast<u8>(ch));
  266. }
  267. void VirtualConsole::flush_dirty_lines()
  268. {
  269. if (!m_active)
  270. return;
  271. VERIFY(GraphicsManagement::is_initialized());
  272. VERIFY(GraphicsManagement::the().console());
  273. for (u16 visual_row = 0; visual_row < rows(); ++visual_row) {
  274. auto& line = m_lines[visual_row];
  275. if (!line.dirty && !m_console_impl.m_need_full_flush)
  276. continue;
  277. for (size_t column = 0; column < columns(); ++column) {
  278. auto& cell = cell_at(column, visual_row);
  279. auto foreground_color = terminal_to_standard_color(cell.attribute.effective_foreground_color());
  280. if (cell.attribute.flags & VT::Attribute::Flags::Bold)
  281. foreground_color = (Graphics::Console::Color)((u8)foreground_color | 0x08);
  282. GraphicsManagement::the().console()->write(column,
  283. visual_row,
  284. ((u8)cell.ch < 128 ? cell.ch : '?'),
  285. terminal_to_standard_color(cell.attribute.effective_background_color()),
  286. foreground_color);
  287. }
  288. line.dirty = false;
  289. }
  290. GraphicsManagement::the().console()->set_cursor(m_console_impl.cursor_column(), m_console_impl.cursor_row());
  291. m_console_impl.m_need_full_flush = false;
  292. }
  293. void VirtualConsole::beep()
  294. {
  295. // TODO
  296. dbgln("Beep!1");
  297. }
  298. void VirtualConsole::set_window_title(const StringView&)
  299. {
  300. // Do nothing.
  301. }
  302. void VirtualConsole::set_window_progress(int, int)
  303. {
  304. // Do nothing.
  305. }
  306. void VirtualConsole::terminal_did_resize(u16 columns, u16 rows)
  307. {
  308. // FIXME: Allocate more Region(s) or deallocate them if needed...
  309. dbgln("VC {}: Resized to {} x {}", index(), columns, rows);
  310. }
  311. void VirtualConsole::terminal_history_changed(int)
  312. {
  313. // Do nothing, I guess?
  314. }
  315. void VirtualConsole::emit(const u8* data, size_t size)
  316. {
  317. for (size_t i = 0; i < size; i++)
  318. TTY::emit(data[i], true);
  319. }
  320. void VirtualConsole::set_cursor_style(VT::CursorStyle)
  321. {
  322. // Do nothing
  323. }
  324. String VirtualConsole::device_name() const
  325. {
  326. return String::formatted("tty{}", minor());
  327. }
  328. void VirtualConsole::echo(u8 ch)
  329. {
  330. m_console_impl.on_input(ch);
  331. if (m_active)
  332. flush_dirty_lines();
  333. }
  334. VirtualConsole::Cell& VirtualConsole::cell_at(size_t x, size_t y)
  335. {
  336. auto* ptr = (VirtualConsole::Cell*)(m_cells->vaddr().as_ptr());
  337. ptr += (y * columns()) + x;
  338. return *ptr;
  339. }
  340. void VirtualConsole::clear()
  341. {
  342. auto* cell = (Cell*)m_cells->vaddr().as_ptr();
  343. for (size_t y = 0; y < rows(); y++) {
  344. m_lines[y].dirty = true;
  345. for (size_t x = 0; x < columns(); x++) {
  346. cell[x].clear();
  347. }
  348. cell += columns();
  349. }
  350. m_console_impl.set_cursor(0, 0);
  351. }
  352. void VirtualConsole::scroll_up(u16 region_top, u16 region_bottom, size_t count)
  353. {
  354. VERIFY(region_top <= region_bottom);
  355. size_t region_size = region_bottom - region_top + 1;
  356. count = min(count, region_size);
  357. size_t line_bytes = (columns() * sizeof(Cell));
  358. memmove(m_cells->vaddr().offset(line_bytes * region_top).as_ptr(), m_cells->vaddr().offset(line_bytes * (region_top + count)).as_ptr(), line_bytes * (region_size - count));
  359. for (size_t i = 0; i < count; ++i)
  360. clear_line(region_bottom - i);
  361. for (u16 row = region_top; row <= region_bottom; ++row)
  362. m_lines[row].dirty = true;
  363. }
  364. void VirtualConsole::scroll_down(u16 region_top, u16 region_bottom, size_t count)
  365. {
  366. VERIFY(region_top <= region_bottom);
  367. size_t region_size = region_bottom - region_top + 1;
  368. count = min(count, region_size);
  369. size_t line_bytes = (columns() * sizeof(Cell));
  370. memmove(m_cells->vaddr().offset(line_bytes * (region_top + count)).as_ptr(), m_cells->vaddr().offset(line_bytes * region_top).as_ptr(), line_bytes * (region_size - count));
  371. for (size_t i = 0; i < count; ++i)
  372. clear_line(region_top + i);
  373. for (u16 row = region_top; row <= region_bottom; ++row)
  374. m_lines[row].dirty = true;
  375. }
  376. void VirtualConsole::scroll_left(u16 row, u16 column, size_t count)
  377. {
  378. VERIFY(row < rows());
  379. VERIFY(column < columns());
  380. count = min<size_t>(count, columns() - column);
  381. memmove(&cell_at(column, row), &cell_at(column + count, row), sizeof(Cell) * (columns() - column - count));
  382. for (size_t i = column + count; i < columns(); ++i)
  383. cell_at(i, row).clear();
  384. m_lines[row].dirty = true;
  385. }
  386. void VirtualConsole::scroll_right(u16 row, u16 column, size_t count)
  387. {
  388. VERIFY(row < rows());
  389. VERIFY(column < columns());
  390. count = min<size_t>(count, columns() - column);
  391. memmove(&cell_at(column + count, row), &cell_at(column, row), sizeof(Cell) * (columns() - column - count));
  392. for (size_t i = column; i < column + count; ++i)
  393. cell_at(i, row).clear();
  394. m_lines[row].dirty = true;
  395. }
  396. void VirtualConsole::clear_in_line(u16 row, u16 first_column, u16 last_column)
  397. {
  398. VERIFY(row < rows());
  399. VERIFY(first_column <= last_column);
  400. VERIFY(last_column < columns());
  401. m_lines[row].dirty = true;
  402. for (size_t x = first_column; x <= last_column; x++)
  403. cell_at(x, row).clear();
  404. }
  405. void VirtualConsole::put_character_at(unsigned row, unsigned column, u32 code_point, const VT::Attribute& attribute)
  406. {
  407. VERIFY(row < rows());
  408. VERIFY(column < columns());
  409. auto& line = m_lines[row];
  410. auto& cell = cell_at(column, row);
  411. cell.attribute.foreground_color = attribute.foreground_color;
  412. cell.attribute.background_color = attribute.background_color;
  413. cell.attribute.flags = attribute.flags;
  414. if (code_point > 128)
  415. cell.ch = ' ';
  416. else
  417. cell.ch = code_point;
  418. cell.attribute.flags |= VT::Attribute::Flags::Touched;
  419. line.dirty = true;
  420. // FIXME: Maybe we should consider to change length after printing a special char in a column
  421. if (code_point <= 20)
  422. return;
  423. line.length = max<size_t>(line.length, column);
  424. }
  425. void VirtualConsole::invalidate_cursor(size_t row)
  426. {
  427. m_lines[row].dirty = true;
  428. }
  429. }