VirtualConsole.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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::ICH(Parameters)
  73. {
  74. // FIXME: Implement this
  75. }
  76. void ConsoleImpl::DCH(Parameters)
  77. {
  78. // FIXME: Implement this
  79. }
  80. void VirtualConsole::set_graphical(bool graphical)
  81. {
  82. m_graphical = graphical;
  83. }
  84. UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create(size_t index)
  85. {
  86. return adopt_ref(*new VirtualConsole(index));
  87. }
  88. UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create_with_preset_log(size_t index, const CircularQueue<char, 16384>& log)
  89. {
  90. return adopt_ref(*new VirtualConsole(index, log));
  91. }
  92. UNMAP_AFTER_INIT void VirtualConsole::initialize()
  93. {
  94. m_tty_name = String::formatted("/dev/tty{}", m_index);
  95. VERIFY(GraphicsManagement::the().console());
  96. set_size(GraphicsManagement::the().console()->max_column(), GraphicsManagement::the().console()->max_row());
  97. m_console_impl.set_size(GraphicsManagement::the().console()->max_column(), GraphicsManagement::the().console()->max_row());
  98. // Allocate twice of the max row * max column * sizeof(Cell) to ensure we can have some sort of history mechanism...
  99. auto size = GraphicsManagement::the().console()->max_column() * GraphicsManagement::the().console()->max_row() * sizeof(Cell) * 2;
  100. m_cells = MM.allocate_kernel_region(page_round_up(size), "Virtual Console Cells", Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow);
  101. // Add the lines, so we also ensure they will be flushed now
  102. for (size_t row = 0; row < rows(); row++) {
  103. m_lines.append({ true, 0 });
  104. }
  105. clear();
  106. VERIFY(m_cells);
  107. }
  108. void VirtualConsole::refresh_after_resolution_change()
  109. {
  110. auto old_rows_count = rows();
  111. auto old_columns_count = columns();
  112. set_size(GraphicsManagement::the().console()->max_column(), GraphicsManagement::the().console()->max_row());
  113. m_console_impl.set_size(GraphicsManagement::the().console()->max_column(), GraphicsManagement::the().console()->max_row());
  114. // Note: From now on, columns() and rows() are updated with the new settings.
  115. auto size = GraphicsManagement::the().console()->max_column() * GraphicsManagement::the().console()->max_row() * sizeof(Cell) * 2;
  116. auto new_cells = MM.allocate_kernel_region(page_round_up(size), "Virtual Console Cells", Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow);
  117. if (rows() < old_rows_count) {
  118. m_lines.shrink(rows());
  119. } else {
  120. for (size_t row = 0; row < (size_t)(rows() - old_rows_count); row++) {
  121. m_lines.append({ true, 0 });
  122. }
  123. }
  124. // Note: A potential loss of displayed data occur when resolution width shrinks.
  125. if (columns() < old_columns_count) {
  126. for (size_t row = 0; row < rows(); row++) {
  127. auto& line = m_lines[row];
  128. 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));
  129. line.dirty = true;
  130. }
  131. } else {
  132. // Handle Growth of resolution
  133. for (size_t row = 0; row < rows(); row++) {
  134. auto& line = m_lines[row];
  135. 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));
  136. line.dirty = true;
  137. }
  138. }
  139. // Update the new cells Region
  140. m_cells = move(new_cells);
  141. m_console_impl.m_need_full_flush = true;
  142. flush_dirty_lines();
  143. }
  144. UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index)
  145. : TTY(4, index)
  146. , m_index(index)
  147. , m_console_impl(*this)
  148. {
  149. initialize();
  150. }
  151. UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index, const CircularQueue<char, 16384>& log)
  152. : TTY(4, index)
  153. , m_index(index)
  154. , m_console_impl(*this)
  155. {
  156. initialize();
  157. // HACK: We have to go through the TTY layer for correct newline handling.
  158. // It would be nice to not have to make all these calls, but we can't get the underlying data pointer
  159. // and head index. If we did that, we could reduce this to at most 2 calls.
  160. for (auto ch : log) {
  161. emit_char(ch);
  162. }
  163. }
  164. UNMAP_AFTER_INIT VirtualConsole::~VirtualConsole()
  165. {
  166. VERIFY_NOT_REACHED();
  167. }
  168. static inline Graphics::Console::Color ansi_color_to_standard_vga_color(VT::Color::ANSIColor color)
  169. {
  170. switch (color) {
  171. case VT::Color::ANSIColor::DefaultBackground:
  172. case VT::Color::ANSIColor::Black:
  173. return Graphics::Console::Color::Black;
  174. case VT::Color::ANSIColor::Red:
  175. return Graphics::Console::Color::Red;
  176. case VT::Color::ANSIColor::Green:
  177. return Graphics::Console::Green;
  178. case VT::Color::ANSIColor::Yellow:
  179. // VGA only has bright yellow, and treats normal yellow as a brownish orange color.
  180. return Graphics::Console::Color::Brown;
  181. case VT::Color::ANSIColor::Blue:
  182. return Graphics::Console::Color::Blue;
  183. case VT::Color::ANSIColor::Magenta:
  184. return Graphics::Console::Color::Magenta;
  185. case VT::Color::ANSIColor::Cyan:
  186. return Graphics::Console::Color::Cyan;
  187. case VT::Color::ANSIColor::DefaultForeground:
  188. case VT::Color::ANSIColor::White:
  189. return Graphics::Console::Color::White;
  190. case VT::Color::ANSIColor::BrightBlack:
  191. return Graphics::Console::Color::LightGray;
  192. case VT::Color::ANSIColor::BrightRed:
  193. return Graphics::Console::Color::BrightRed;
  194. case VT::Color::ANSIColor::BrightGreen:
  195. return Graphics::Console::Color::BrightGreen;
  196. case VT::Color::ANSIColor::BrightYellow:
  197. return Graphics::Console::Color::Yellow;
  198. case VT::Color::ANSIColor::BrightBlue:
  199. return Graphics::Console::Color::BrightBlue;
  200. case VT::Color::ANSIColor::BrightMagenta:
  201. return Graphics::Console::Color::BrightMagenta;
  202. case VT::Color::ANSIColor::BrightCyan:
  203. return Graphics::Console::Color::BrightCyan;
  204. default:
  205. VERIFY_NOT_REACHED();
  206. }
  207. }
  208. static inline Graphics::Console::Color terminal_to_standard_color(VT::Color color)
  209. {
  210. switch (color.kind()) {
  211. case VT::Color::Kind::Named:
  212. return ansi_color_to_standard_vga_color(color.as_named());
  213. default:
  214. return Graphics::Console::Color::LightGray;
  215. }
  216. }
  217. void VirtualConsole::on_key_pressed(KeyEvent event)
  218. {
  219. // Ignore keyboard in graphical mode.
  220. if (m_graphical)
  221. return;
  222. if (!event.is_press())
  223. return;
  224. Processor::deferred_call_queue([this, event]() {
  225. m_console_impl.handle_key_press(event.key, event.code_point, event.flags);
  226. });
  227. }
  228. ssize_t VirtualConsole::on_tty_write(const UserOrKernelBuffer& data, ssize_t size)
  229. {
  230. ScopedSpinLock global_lock(ConsoleManagement::the().tty_write_lock());
  231. ScopedSpinLock lock(m_lock);
  232. auto result = data.read_buffered<512>((size_t)size, [&](u8 const* buffer, size_t buffer_bytes) {
  233. for (size_t i = 0; i < buffer_bytes; ++i)
  234. m_console_impl.on_input(buffer[i]);
  235. return buffer_bytes;
  236. });
  237. if (m_active)
  238. flush_dirty_lines();
  239. if (result.is_error())
  240. return result.error();
  241. return (ssize_t)result.value();
  242. }
  243. void VirtualConsole::set_active(bool active)
  244. {
  245. VERIFY(ConsoleManagement::the().m_lock.is_locked());
  246. VERIFY(m_active != active);
  247. m_active = active;
  248. if (active) {
  249. HIDManagement::the().set_client(this);
  250. m_console_impl.m_need_full_flush = true;
  251. flush_dirty_lines();
  252. } else {
  253. HIDManagement::the().set_client(nullptr);
  254. }
  255. }
  256. void VirtualConsole::emit_char(char ch)
  257. {
  258. // Since we are standards-compliant by not moving to column 1 on '\n', we have to add an extra carriage return to
  259. // do newlines properly. The `TTY` layer handles adding it.
  260. echo_with_processing(static_cast<u8>(ch));
  261. }
  262. void VirtualConsole::flush_dirty_lines()
  263. {
  264. if (!m_active)
  265. return;
  266. VERIFY(GraphicsManagement::is_initialized());
  267. VERIFY(GraphicsManagement::the().console());
  268. for (u16 visual_row = 0; visual_row < rows(); ++visual_row) {
  269. auto& line = m_lines[visual_row];
  270. if (!line.dirty && !m_console_impl.m_need_full_flush)
  271. continue;
  272. for (size_t column = 0; column < columns(); ++column) {
  273. auto& cell = cell_at(column, visual_row);
  274. auto foreground_color = terminal_to_standard_color(cell.attribute.effective_foreground_color());
  275. if (cell.attribute.flags & VT::Attribute::Flags::Bold)
  276. foreground_color = (Graphics::Console::Color)((u8)foreground_color | 0x08);
  277. GraphicsManagement::the().console()->write(column,
  278. visual_row,
  279. ((u8)cell.ch < 128 ? cell.ch : '?'),
  280. terminal_to_standard_color(cell.attribute.effective_background_color()),
  281. foreground_color);
  282. }
  283. line.dirty = false;
  284. }
  285. GraphicsManagement::the().console()->set_cursor(m_console_impl.cursor_column(), m_console_impl.cursor_row());
  286. m_console_impl.m_need_full_flush = false;
  287. }
  288. void VirtualConsole::beep()
  289. {
  290. // TODO
  291. dbgln("Beep!1");
  292. }
  293. void VirtualConsole::set_window_title(const StringView&)
  294. {
  295. // Do nothing.
  296. }
  297. void VirtualConsole::set_window_progress(int, int)
  298. {
  299. // Do nothing.
  300. }
  301. void VirtualConsole::terminal_did_resize(u16 columns, u16 rows)
  302. {
  303. // FIXME: Allocate more Region(s) or deallocate them if needed...
  304. dbgln("VC {}: Resized to {} x {}", index(), columns, rows);
  305. }
  306. void VirtualConsole::terminal_history_changed()
  307. {
  308. // Do nothing, I guess?
  309. }
  310. void VirtualConsole::emit(const u8* data, size_t size)
  311. {
  312. for (size_t i = 0; i < size; i++)
  313. TTY::emit(data[i], true);
  314. }
  315. void VirtualConsole::set_cursor_style(VT::CursorStyle)
  316. {
  317. // Do nothing
  318. }
  319. String VirtualConsole::device_name() const
  320. {
  321. return String::formatted("tty{}", minor());
  322. }
  323. void VirtualConsole::echo(u8 ch)
  324. {
  325. m_console_impl.on_input(ch);
  326. }
  327. VirtualConsole::Cell& VirtualConsole::cell_at(size_t x, size_t y)
  328. {
  329. auto* ptr = (VirtualConsole::Cell*)(m_cells->vaddr().as_ptr());
  330. ptr += (y * columns()) + x;
  331. return *ptr;
  332. }
  333. void VirtualConsole::clear()
  334. {
  335. auto* cell = (Cell*)m_cells->vaddr().as_ptr();
  336. for (size_t y = 0; y < rows(); y++) {
  337. m_lines[y].dirty = true;
  338. for (size_t x = 0; x < columns(); x++) {
  339. cell[x].clear();
  340. }
  341. cell += columns();
  342. }
  343. m_console_impl.set_cursor(0, 0);
  344. }
  345. void VirtualConsole::scroll_up(u16 region_top, u16 region_bottom, size_t count)
  346. {
  347. VERIFY(region_top <= region_bottom);
  348. size_t region_size = region_bottom - region_top + 1;
  349. count = min(count, region_size);
  350. size_t line_bytes = (columns() * sizeof(Cell));
  351. 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));
  352. for (size_t i = 0; i < count; ++i)
  353. clear_line(region_bottom - i);
  354. for (u16 row = region_top; row <= region_bottom; ++row)
  355. m_lines[row].dirty = true;
  356. }
  357. void VirtualConsole::scroll_down(u16 region_top, u16 region_bottom, size_t count)
  358. {
  359. VERIFY(region_top <= region_bottom);
  360. size_t region_size = region_bottom - region_top + 1;
  361. count = min(count, region_size);
  362. size_t line_bytes = (columns() * sizeof(Cell));
  363. 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));
  364. for (size_t i = 0; i < count; ++i)
  365. clear_line(region_top + i);
  366. for (u16 row = region_top; row <= region_bottom; ++row)
  367. m_lines[row].dirty = true;
  368. }
  369. void VirtualConsole::clear_line(size_t y_index)
  370. {
  371. m_lines[y_index].dirty = true;
  372. for (size_t x = 0; x < columns(); x++) {
  373. auto& cell = cell_at(x, y_index);
  374. cell.clear();
  375. }
  376. }
  377. void VirtualConsole::put_character_at(unsigned row, unsigned column, u32 code_point, const VT::Attribute& attribute)
  378. {
  379. VERIFY(row < rows());
  380. VERIFY(column < columns());
  381. auto& line = m_lines[row];
  382. auto& cell = cell_at(column, row);
  383. cell.attribute.foreground_color = attribute.foreground_color;
  384. cell.attribute.background_color = attribute.background_color;
  385. cell.attribute.flags = attribute.flags;
  386. if (code_point > 128)
  387. cell.ch = ' ';
  388. else
  389. cell.ch = code_point;
  390. cell.attribute.flags |= VT::Attribute::Flags::Touched;
  391. line.dirty = true;
  392. // FIXME: Maybe we should consider to change length after printing a special char in a column
  393. if (code_point <= 20)
  394. return;
  395. line.length = max<size_t>(line.length, column);
  396. }
  397. void VirtualConsole::invalidate_cursor(size_t row)
  398. {
  399. m_lines[row].dirty = true;
  400. }
  401. }