VirtualConsole.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. auto common_rows_count = min(old_rows_count, rows());
  130. auto common_columns_count = min(old_columns_count, columns());
  131. for (size_t row = 0; row < common_rows_count; row++) {
  132. auto& line = m_lines[row];
  133. memcpy(new_cells->vaddr().offset(row * columns() * sizeof(Cell)).as_ptr(), m_cells->vaddr().offset(row * old_columns_count * sizeof(Cell)).as_ptr(), common_columns_count * sizeof(Cell));
  134. line.dirty = true;
  135. }
  136. // Update the new cells Region
  137. m_cells = move(new_cells);
  138. m_console_impl.m_need_full_flush = true;
  139. flush_dirty_lines();
  140. }
  141. UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index)
  142. : TTY(4, index)
  143. , m_index(index)
  144. , m_console_impl(*this)
  145. {
  146. initialize();
  147. }
  148. UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index, const CircularQueue<char, 16384>& log)
  149. : TTY(4, index)
  150. , m_index(index)
  151. , m_console_impl(*this)
  152. {
  153. initialize();
  154. // HACK: We have to go through the TTY layer for correct newline handling.
  155. // It would be nice to not have to make all these calls, but we can't get the underlying data pointer
  156. // and head index. If we did that, we could reduce this to at most 2 calls.
  157. for (auto ch : log) {
  158. emit_char(ch);
  159. }
  160. }
  161. UNMAP_AFTER_INIT VirtualConsole::~VirtualConsole()
  162. {
  163. VERIFY_NOT_REACHED();
  164. }
  165. static inline Graphics::Console::Color ansi_color_to_standard_vga_color(VT::Color::ANSIColor color)
  166. {
  167. switch (color) {
  168. case VT::Color::ANSIColor::DefaultBackground:
  169. case VT::Color::ANSIColor::Black:
  170. return Graphics::Console::Color::Black;
  171. case VT::Color::ANSIColor::Red:
  172. return Graphics::Console::Color::Red;
  173. case VT::Color::ANSIColor::Green:
  174. return Graphics::Console::Color::Green;
  175. case VT::Color::ANSIColor::Yellow:
  176. // VGA only has bright yellow, and treats normal yellow as a brownish orange color.
  177. return Graphics::Console::Color::Brown;
  178. case VT::Color::ANSIColor::Blue:
  179. return Graphics::Console::Color::Blue;
  180. case VT::Color::ANSIColor::Magenta:
  181. return Graphics::Console::Color::Magenta;
  182. case VT::Color::ANSIColor::Cyan:
  183. return Graphics::Console::Color::Cyan;
  184. case VT::Color::ANSIColor::DefaultForeground:
  185. case VT::Color::ANSIColor::White:
  186. return Graphics::Console::Color::LightGray;
  187. case VT::Color::ANSIColor::BrightBlack:
  188. return Graphics::Console::Color::DarkGray;
  189. case VT::Color::ANSIColor::BrightRed:
  190. return Graphics::Console::Color::BrightRed;
  191. case VT::Color::ANSIColor::BrightGreen:
  192. return Graphics::Console::Color::BrightGreen;
  193. case VT::Color::ANSIColor::BrightYellow:
  194. return Graphics::Console::Color::Yellow;
  195. case VT::Color::ANSIColor::BrightBlue:
  196. return Graphics::Console::Color::BrightBlue;
  197. case VT::Color::ANSIColor::BrightMagenta:
  198. return Graphics::Console::Color::BrightMagenta;
  199. case VT::Color::ANSIColor::BrightCyan:
  200. return Graphics::Console::Color::BrightCyan;
  201. case VT::Color::ANSIColor::BrightWhite:
  202. return Graphics::Console::Color::White;
  203. }
  204. VERIFY_NOT_REACHED();
  205. }
  206. static inline Graphics::Console::Color terminal_to_standard_color(VT::Color color)
  207. {
  208. switch (color.kind()) {
  209. case VT::Color::Kind::Named:
  210. return ansi_color_to_standard_vga_color(color.as_named());
  211. default:
  212. return Graphics::Console::Color::LightGray;
  213. }
  214. }
  215. void VirtualConsole::on_key_pressed(KeyEvent event)
  216. {
  217. // Ignore keyboard in graphical mode.
  218. if (m_graphical)
  219. return;
  220. if (!event.is_press())
  221. return;
  222. Processor::deferred_call_queue([this, event]() {
  223. m_console_impl.handle_key_press(event.key, event.code_point, event.flags);
  224. });
  225. }
  226. KResultOr<size_t> VirtualConsole::on_tty_write(const UserOrKernelBuffer& data, size_t size)
  227. {
  228. ScopedSpinLock global_lock(ConsoleManagement::the().tty_write_lock());
  229. ScopedSpinLock lock(m_lock);
  230. auto result = data.read_buffered<512>(size, [&](u8 const* buffer, size_t buffer_bytes) {
  231. for (size_t i = 0; i < buffer_bytes; ++i)
  232. m_console_impl.on_input(buffer[i]);
  233. return buffer_bytes;
  234. });
  235. if (m_active)
  236. flush_dirty_lines();
  237. return result;
  238. }
  239. void VirtualConsole::set_active(bool active)
  240. {
  241. VERIFY(ConsoleManagement::the().m_lock.is_locked());
  242. VERIFY(m_active != active);
  243. m_active = active;
  244. if (active) {
  245. HIDManagement::the().set_client(this);
  246. m_console_impl.m_need_full_flush = true;
  247. flush_dirty_lines();
  248. } else {
  249. HIDManagement::the().set_client(nullptr);
  250. }
  251. }
  252. void VirtualConsole::emit_char(char ch)
  253. {
  254. // Since we are standards-compliant by not moving to column 1 on '\n', we have to add an extra carriage return to
  255. // do newlines properly. The `TTY` layer handles adding it.
  256. echo_with_processing(static_cast<u8>(ch));
  257. }
  258. void VirtualConsole::flush_dirty_lines()
  259. {
  260. if (!m_active)
  261. return;
  262. VERIFY(GraphicsManagement::is_initialized());
  263. VERIFY(GraphicsManagement::the().console());
  264. for (u16 visual_row = 0; visual_row < rows(); ++visual_row) {
  265. auto& line = m_lines[visual_row];
  266. if (!line.dirty && !m_console_impl.m_need_full_flush)
  267. continue;
  268. for (size_t column = 0; column < columns(); ++column) {
  269. auto& cell = cell_at(column, visual_row);
  270. auto foreground_color = terminal_to_standard_color(cell.attribute.effective_foreground_color());
  271. if (cell.attribute.flags & VT::Attribute::Flags::Bold)
  272. foreground_color = (Graphics::Console::Color)((u8)foreground_color | 0x08);
  273. GraphicsManagement::the().console()->write(column,
  274. visual_row,
  275. ((u8)cell.ch < 128 ? cell.ch : '?'),
  276. terminal_to_standard_color(cell.attribute.effective_background_color()),
  277. foreground_color);
  278. }
  279. line.dirty = false;
  280. }
  281. GraphicsManagement::the().console()->set_cursor(m_console_impl.cursor_column(), m_console_impl.cursor_row());
  282. m_console_impl.m_need_full_flush = false;
  283. }
  284. void VirtualConsole::beep()
  285. {
  286. // TODO
  287. dbgln("Beep!1");
  288. }
  289. void VirtualConsole::set_window_title(const StringView&)
  290. {
  291. // Do nothing.
  292. }
  293. void VirtualConsole::set_window_progress(int, int)
  294. {
  295. // Do nothing.
  296. }
  297. void VirtualConsole::terminal_did_resize(u16 columns, u16 rows)
  298. {
  299. // FIXME: Allocate more Region(s) or deallocate them if needed...
  300. dbgln("VC {}: Resized to {} x {}", index(), columns, rows);
  301. }
  302. void VirtualConsole::terminal_history_changed(int)
  303. {
  304. // Do nothing, I guess?
  305. }
  306. void VirtualConsole::emit(const u8* data, size_t size)
  307. {
  308. for (size_t i = 0; i < size; i++)
  309. TTY::emit(data[i], true);
  310. }
  311. void VirtualConsole::set_cursor_style(VT::CursorStyle)
  312. {
  313. // Do nothing
  314. }
  315. String VirtualConsole::device_name() const
  316. {
  317. return String::formatted("tty{}", minor());
  318. }
  319. void VirtualConsole::echo(u8 ch)
  320. {
  321. m_console_impl.on_input(ch);
  322. if (m_active)
  323. flush_dirty_lines();
  324. }
  325. VirtualConsole::Cell& VirtualConsole::cell_at(size_t x, size_t y)
  326. {
  327. auto* ptr = (VirtualConsole::Cell*)(m_cells->vaddr().as_ptr());
  328. ptr += (y * columns()) + x;
  329. return *ptr;
  330. }
  331. void VirtualConsole::clear()
  332. {
  333. auto* cell = (Cell*)m_cells->vaddr().as_ptr();
  334. for (size_t y = 0; y < rows(); y++) {
  335. m_lines[y].dirty = true;
  336. for (size_t x = 0; x < columns(); x++) {
  337. cell[x].clear();
  338. }
  339. cell += columns();
  340. }
  341. m_console_impl.set_cursor(0, 0);
  342. }
  343. void VirtualConsole::scroll_up(u16 region_top, u16 region_bottom, size_t count)
  344. {
  345. VERIFY(region_top <= region_bottom);
  346. size_t region_size = region_bottom - region_top + 1;
  347. count = min(count, region_size);
  348. size_t line_bytes = (columns() * sizeof(Cell));
  349. 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));
  350. for (size_t i = 0; i < count; ++i)
  351. clear_line(region_bottom - i);
  352. for (u16 row = region_top; row <= region_bottom; ++row)
  353. m_lines[row].dirty = true;
  354. }
  355. void VirtualConsole::scroll_down(u16 region_top, u16 region_bottom, size_t count)
  356. {
  357. VERIFY(region_top <= region_bottom);
  358. size_t region_size = region_bottom - region_top + 1;
  359. count = min(count, region_size);
  360. size_t line_bytes = (columns() * sizeof(Cell));
  361. 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));
  362. for (size_t i = 0; i < count; ++i)
  363. clear_line(region_top + i);
  364. for (u16 row = region_top; row <= region_bottom; ++row)
  365. m_lines[row].dirty = true;
  366. }
  367. void VirtualConsole::scroll_left(u16 row, u16 column, size_t count)
  368. {
  369. VERIFY(row < rows());
  370. VERIFY(column < columns());
  371. count = min<size_t>(count, columns() - column);
  372. memmove(&cell_at(column, row), &cell_at(column + count, row), sizeof(Cell) * (columns() - column - count));
  373. for (size_t i = column + count; i < columns(); ++i)
  374. cell_at(i, row).clear();
  375. m_lines[row].dirty = true;
  376. }
  377. void VirtualConsole::scroll_right(u16 row, u16 column, size_t count)
  378. {
  379. VERIFY(row < rows());
  380. VERIFY(column < columns());
  381. count = min<size_t>(count, columns() - column);
  382. memmove(&cell_at(column + count, row), &cell_at(column, row), sizeof(Cell) * (columns() - column - count));
  383. for (size_t i = column; i < column + count; ++i)
  384. cell_at(i, row).clear();
  385. m_lines[row].dirty = true;
  386. }
  387. void VirtualConsole::clear_in_line(u16 row, u16 first_column, u16 last_column)
  388. {
  389. VERIFY(row < rows());
  390. VERIFY(first_column <= last_column);
  391. VERIFY(last_column < columns());
  392. m_lines[row].dirty = true;
  393. for (size_t x = first_column; x <= last_column; x++)
  394. cell_at(x, row).clear();
  395. }
  396. void VirtualConsole::put_character_at(unsigned row, unsigned column, u32 code_point, const VT::Attribute& attribute)
  397. {
  398. VERIFY(row < rows());
  399. VERIFY(column < columns());
  400. auto& line = m_lines[row];
  401. auto& cell = cell_at(column, row);
  402. cell.attribute.foreground_color = attribute.foreground_color;
  403. cell.attribute.background_color = attribute.background_color;
  404. cell.attribute.flags = attribute.flags;
  405. if (code_point > 128)
  406. cell.ch = ' ';
  407. else
  408. cell.ch = code_point;
  409. cell.attribute.flags |= VT::Attribute::Flags::Touched;
  410. line.dirty = true;
  411. // FIXME: Maybe we should consider to change length after printing a special char in a column
  412. if (code_point <= 20)
  413. return;
  414. line.length = max<size_t>(line.length, column);
  415. }
  416. void VirtualConsole::invalidate_cursor(size_t row)
  417. {
  418. m_lines[row].dirty = true;
  419. }
  420. }