VirtualConsole.cpp 16 KB

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