VirtualConsole.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. namespace Kernel {
  20. ConsoleImpl::ConsoleImpl(VirtualConsole& client)
  21. : Terminal(client)
  22. {
  23. }
  24. void ConsoleImpl::invalidate_cursor()
  25. {
  26. }
  27. void ConsoleImpl::clear()
  28. {
  29. m_client.clear();
  30. }
  31. void ConsoleImpl::clear_including_history()
  32. {
  33. }
  34. void ConsoleImpl::set_size(u16 determined_columns, u16 determined_rows)
  35. {
  36. VERIFY(determined_columns);
  37. VERIFY(determined_rows);
  38. if (determined_columns == columns() && determined_rows == rows())
  39. return;
  40. m_columns = determined_columns;
  41. m_rows = determined_rows;
  42. m_cursor_row = min<size_t>((int)m_cursor_row, rows() - 1);
  43. m_cursor_column = min<size_t>((int)m_cursor_column, columns() - 1);
  44. m_saved_cursor_row = min<size_t>((int)m_saved_cursor_row, rows() - 1);
  45. m_saved_cursor_column = min<size_t>((int)m_saved_cursor_column, columns() - 1);
  46. m_horizontal_tabs.resize(determined_columns);
  47. for (unsigned i = 0; i < determined_columns; ++i)
  48. m_horizontal_tabs[i] = (i % 8) == 0;
  49. // Rightmost column is always last tab on line.
  50. m_horizontal_tabs[determined_columns - 1] = 1;
  51. m_client.terminal_did_resize(m_columns, m_rows);
  52. }
  53. void ConsoleImpl::scroll_up()
  54. {
  55. // NOTE: We have to invalidate the cursor first.
  56. m_client.invalidate_cursor(m_cursor_row);
  57. m_client.scroll_up();
  58. }
  59. void ConsoleImpl::scroll_down()
  60. {
  61. }
  62. void ConsoleImpl::linefeed()
  63. {
  64. u16 new_row = m_cursor_row;
  65. u16 max_row = rows() - 1;
  66. if (new_row == max_row) {
  67. // NOTE: We have to invalidate the cursor first.
  68. m_client.invalidate_cursor(new_row);
  69. m_client.scroll_up();
  70. } else {
  71. ++new_row;
  72. }
  73. set_cursor(new_row, 0);
  74. }
  75. void ConsoleImpl::put_character_at(unsigned row, unsigned column, u32 ch)
  76. {
  77. m_client.put_character_at(row, column, ch, m_current_attribute);
  78. m_last_code_point = ch;
  79. }
  80. void ConsoleImpl::set_window_title(const String&)
  81. {
  82. }
  83. void ConsoleImpl::ICH(Parameters)
  84. {
  85. // FIXME: Implement this
  86. }
  87. void ConsoleImpl::IL(Parameters)
  88. {
  89. // FIXME: Implement this
  90. }
  91. void ConsoleImpl::DCH(Parameters)
  92. {
  93. // FIXME: Implement this
  94. }
  95. void ConsoleImpl::DL(Parameters)
  96. {
  97. // FIXME: Implement this
  98. }
  99. void VirtualConsole::set_graphical(bool graphical)
  100. {
  101. m_graphical = graphical;
  102. }
  103. UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create(size_t index)
  104. {
  105. return adopt_ref(*new VirtualConsole(index));
  106. }
  107. UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create_with_preset_log(size_t index, const CircularQueue<char, 16384>& log)
  108. {
  109. return adopt_ref(*new VirtualConsole(index, log));
  110. }
  111. UNMAP_AFTER_INIT void VirtualConsole::initialize()
  112. {
  113. m_tty_name = String::formatted("/dev/tty{}", m_index);
  114. VERIFY(GraphicsManagement::the().console());
  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. // Allocate twice of the max row * max column * sizeof(Cell) to ensure we can have some sort of history mechanism...
  118. auto size = GraphicsManagement::the().console()->max_column() * GraphicsManagement::the().console()->max_row() * sizeof(Cell) * 2;
  119. m_cells = MM.allocate_kernel_region(page_round_up(size), "Virtual Console Cells", Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow);
  120. // Add the lines, so we also ensure they will be flushed now
  121. for (size_t row = 0; row < rows(); row++) {
  122. m_lines.append({ true, 0 });
  123. }
  124. clear();
  125. VERIFY(m_cells);
  126. }
  127. void VirtualConsole::refresh_after_resolution_change()
  128. {
  129. auto old_rows_count = rows();
  130. auto old_columns_count = columns();
  131. set_size(GraphicsManagement::the().console()->max_column(), GraphicsManagement::the().console()->max_row());
  132. m_console_impl.set_size(GraphicsManagement::the().console()->max_column(), GraphicsManagement::the().console()->max_row());
  133. // Note: From now on, columns() and rows() are updated with the new settings.
  134. auto size = GraphicsManagement::the().console()->max_column() * GraphicsManagement::the().console()->max_row() * sizeof(Cell) * 2;
  135. auto new_cells = MM.allocate_kernel_region(page_round_up(size), "Virtual Console Cells", Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow);
  136. if (rows() < old_rows_count) {
  137. m_lines.shrink(rows());
  138. } else {
  139. for (size_t row = 0; row < (size_t)(rows() - old_rows_count); row++) {
  140. m_lines.append({ true, 0 });
  141. }
  142. }
  143. // Note: A potential loss of displayed data occur when resolution width shrinks.
  144. if (columns() < old_columns_count) {
  145. for (size_t row = 0; row < rows(); row++) {
  146. auto& line = m_lines[row];
  147. 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));
  148. line.dirty = true;
  149. }
  150. } else {
  151. // Handle Growth of resolution
  152. for (size_t row = 0; row < rows(); row++) {
  153. auto& line = m_lines[row];
  154. 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));
  155. line.dirty = true;
  156. }
  157. }
  158. // Update the new cells Region
  159. m_cells = move(new_cells);
  160. m_console_impl.m_need_full_flush = true;
  161. flush_dirty_lines();
  162. }
  163. UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index)
  164. : TTY(4, index)
  165. , m_index(index)
  166. , m_console_impl(*this)
  167. {
  168. initialize();
  169. }
  170. UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index, const CircularQueue<char, 16384>& log)
  171. : TTY(4, index)
  172. , m_index(index)
  173. , m_console_impl(*this)
  174. {
  175. initialize();
  176. for (auto& ch : log) {
  177. echo(ch);
  178. }
  179. }
  180. UNMAP_AFTER_INIT VirtualConsole::~VirtualConsole()
  181. {
  182. VERIFY_NOT_REACHED();
  183. }
  184. enum class ANSIColor : u8 {
  185. Black = 0,
  186. Red,
  187. Green,
  188. Brown,
  189. Blue,
  190. Magenta,
  191. Cyan,
  192. LightGray,
  193. DarkGray,
  194. BrightRed,
  195. BrightGreen,
  196. Yellow,
  197. BrightBlue,
  198. BrightMagenta,
  199. BrightCyan,
  200. White,
  201. __Count,
  202. };
  203. static inline Graphics::Console::Color ansi_color_to_standard_vga_color(ANSIColor color)
  204. {
  205. switch (color) {
  206. case ANSIColor::Black:
  207. return Graphics::Console::Color::Black;
  208. case ANSIColor::Red:
  209. return Graphics::Console::Color::Red;
  210. case ANSIColor::Brown:
  211. return Graphics::Console::Color::Brown;
  212. case ANSIColor::Blue:
  213. return Graphics::Console::Color::Blue;
  214. case ANSIColor::Magenta:
  215. return Graphics::Console::Color::Magenta;
  216. case ANSIColor::Green:
  217. return Graphics::Console::Color::Green;
  218. case ANSIColor::Cyan:
  219. return Graphics::Console::Color::Cyan;
  220. case ANSIColor::LightGray:
  221. return Graphics::Console::Color::LightGray;
  222. case ANSIColor::DarkGray:
  223. return Graphics::Console::Color::DarkGray;
  224. case ANSIColor::BrightRed:
  225. return Graphics::Console::Color::BrightRed;
  226. case ANSIColor::BrightGreen:
  227. return Graphics::Console::Color::BrightGreen;
  228. case ANSIColor::Yellow:
  229. return Graphics::Console::Color::Yellow;
  230. case ANSIColor::BrightBlue:
  231. return Graphics::Console::Color::BrightBlue;
  232. case ANSIColor::BrightMagenta:
  233. return Graphics::Console::Color::BrightMagenta;
  234. case ANSIColor::BrightCyan:
  235. return Graphics::Console::Color::BrightCyan;
  236. case ANSIColor::White:
  237. return Graphics::Console::Color::White;
  238. default:
  239. VERIFY_NOT_REACHED();
  240. }
  241. }
  242. static inline Graphics::Console::Color xterm_to_standard_color(u32 color)
  243. {
  244. for (u8 i = 0; i < (u8)ANSIColor::__Count; i++) {
  245. if (xterm_colors[i] == color)
  246. return (Graphics::Console::Color)ansi_color_to_standard_vga_color((ANSIColor)i);
  247. }
  248. return Graphics::Console::Color::LightGray;
  249. }
  250. void VirtualConsole::on_key_pressed(KeyEvent event)
  251. {
  252. // Ignore keyboard in graphical mode.
  253. if (m_graphical)
  254. return;
  255. if (!event.is_press())
  256. return;
  257. Processor::deferred_call_queue([this, event]() {
  258. m_console_impl.handle_key_press(event.key, event.code_point, event.flags);
  259. });
  260. }
  261. ssize_t VirtualConsole::on_tty_write(const UserOrKernelBuffer& data, ssize_t size)
  262. {
  263. ScopedSpinLock global_lock(ConsoleManagement::the().tty_write_lock());
  264. ScopedSpinLock lock(m_lock);
  265. auto result = data.read_buffered<512>((size_t)size, [&](u8 const* buffer, size_t buffer_bytes) {
  266. for (size_t i = 0; i < buffer_bytes; ++i)
  267. m_console_impl.on_input(buffer[i]);
  268. return buffer_bytes;
  269. });
  270. if (m_active)
  271. flush_dirty_lines();
  272. if (result.is_error())
  273. return result.error();
  274. return (ssize_t)result.value();
  275. }
  276. void VirtualConsole::set_active(bool active)
  277. {
  278. VERIFY(ConsoleManagement::the().m_lock.is_locked());
  279. VERIFY(m_active != active);
  280. m_active = active;
  281. if (active) {
  282. HIDManagement::the().set_client(this);
  283. m_console_impl.m_need_full_flush = true;
  284. flush_dirty_lines();
  285. } else {
  286. HIDManagement::the().set_client(nullptr);
  287. }
  288. }
  289. void VirtualConsole::emit_char(char ch)
  290. {
  291. echo(ch);
  292. }
  293. void VirtualConsole::flush_dirty_lines()
  294. {
  295. if (!m_active)
  296. return;
  297. VERIFY(GraphicsManagement::is_initialized());
  298. VERIFY(GraphicsManagement::the().console());
  299. for (u16 visual_row = 0; visual_row < rows(); ++visual_row) {
  300. auto& line = m_lines[visual_row];
  301. if (!line.dirty && !m_console_impl.m_need_full_flush)
  302. continue;
  303. for (size_t column = 0; column < columns(); ++column) {
  304. auto& cell = cell_at(column, visual_row);
  305. auto foreground_color = xterm_to_standard_color(cell.attribute.effective_foreground_color());
  306. if (cell.attribute.flags & VT::Attribute::Flags::Bold)
  307. foreground_color = (Graphics::Console::Color)((u8)foreground_color | 0x08);
  308. GraphicsManagement::the().console()->write(column,
  309. visual_row,
  310. ((u8)cell.ch < 128 ? cell.ch : '?'),
  311. xterm_to_standard_color(cell.attribute.effective_background_color()),
  312. foreground_color);
  313. }
  314. line.dirty = false;
  315. }
  316. GraphicsManagement::the().console()->set_cursor(m_console_impl.cursor_column(), m_console_impl.cursor_row());
  317. m_console_impl.m_need_full_flush = false;
  318. }
  319. void VirtualConsole::beep()
  320. {
  321. // TODO
  322. dbgln("Beep!1");
  323. }
  324. void VirtualConsole::set_window_title(const StringView&)
  325. {
  326. // Do nothing.
  327. }
  328. void VirtualConsole::set_window_progress(int, int)
  329. {
  330. // Do nothing.
  331. }
  332. void VirtualConsole::terminal_did_resize(u16 columns, u16 rows)
  333. {
  334. // FIXME: Allocate more Region(s) or deallocate them if needed...
  335. dbgln("VC {}: Resized to {} x {}", index(), columns, rows);
  336. }
  337. void VirtualConsole::terminal_history_changed()
  338. {
  339. // Do nothing, I guess?
  340. }
  341. void VirtualConsole::emit(const u8* data, size_t size)
  342. {
  343. for (size_t i = 0; i < size; i++)
  344. TTY::emit(data[i], true);
  345. }
  346. void VirtualConsole::set_cursor_style(VT::CursorStyle)
  347. {
  348. // Do nothing
  349. }
  350. String VirtualConsole::device_name() const
  351. {
  352. return String::formatted("tty{}", minor());
  353. }
  354. void VirtualConsole::echo(u8 ch)
  355. {
  356. if (should_echo_input()) {
  357. auto buffer = UserOrKernelBuffer::for_kernel_buffer(&ch);
  358. on_tty_write(buffer, 1);
  359. }
  360. }
  361. VirtualConsole::Cell& VirtualConsole::cell_at(size_t x, size_t y)
  362. {
  363. auto* ptr = (VirtualConsole::Cell*)(m_cells->vaddr().as_ptr());
  364. ptr += (y * columns()) + x;
  365. return *ptr;
  366. }
  367. void VirtualConsole::clear()
  368. {
  369. auto* cell = (Cell*)m_cells->vaddr().as_ptr();
  370. for (size_t y = 0; y < rows(); y++) {
  371. m_lines[y].dirty = true;
  372. for (size_t x = 0; x < columns(); x++) {
  373. cell[x].clear();
  374. }
  375. cell += columns();
  376. }
  377. m_console_impl.set_cursor(0, 0);
  378. }
  379. void VirtualConsole::scroll_up()
  380. {
  381. memmove(m_cells->vaddr().as_ptr(), m_cells->vaddr().offset(columns() * sizeof(Cell)).as_ptr(), ((rows() - 1) * columns() * sizeof(Cell)));
  382. clear_line(rows() - 1);
  383. m_console_impl.m_need_full_flush = true;
  384. }
  385. void VirtualConsole::clear_line(size_t y_index)
  386. {
  387. m_lines[y_index].dirty = true;
  388. for (size_t x = 0; x < columns(); x++) {
  389. auto& cell = cell_at(x, y_index);
  390. cell.clear();
  391. }
  392. }
  393. void VirtualConsole::put_character_at(unsigned row, unsigned column, u32 code_point, const VT::Attribute& attribute)
  394. {
  395. VERIFY(row < rows());
  396. VERIFY(column < columns());
  397. auto& line = m_lines[row];
  398. auto& cell = cell_at(column, row);
  399. cell.attribute.foreground_color = attribute.foreground_color;
  400. cell.attribute.background_color = attribute.background_color;
  401. cell.attribute.flags = attribute.flags;
  402. if (code_point > 128)
  403. cell.ch = ' ';
  404. else
  405. cell.ch = code_point;
  406. cell.attribute.flags |= VT::Attribute::Flags::Touched;
  407. line.dirty = true;
  408. // FIXME: Maybe we should consider to change length after printing a special char in a column
  409. if (code_point <= 20)
  410. return;
  411. line.length = max<size_t>(line.length, column);
  412. }
  413. void VirtualConsole::invalidate_cursor(size_t row)
  414. {
  415. m_lines[row].dirty = true;
  416. }
  417. }