VirtualConsole.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 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 });
  123. }
  124. clear();
  125. VERIFY(m_cells);
  126. }
  127. UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index)
  128. : TTY(4, index)
  129. , m_index(index)
  130. , m_console_impl(*this)
  131. {
  132. initialize();
  133. }
  134. UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index, const CircularQueue<char, 16384>& log)
  135. : TTY(4, index)
  136. , m_index(index)
  137. , m_console_impl(*this)
  138. {
  139. initialize();
  140. for (auto& ch : log) {
  141. echo(ch);
  142. }
  143. }
  144. UNMAP_AFTER_INIT VirtualConsole::~VirtualConsole()
  145. {
  146. VERIFY_NOT_REACHED();
  147. }
  148. enum class ANSIColor : u8 {
  149. Black = 0,
  150. Red,
  151. Green,
  152. Brown,
  153. Blue,
  154. Magenta,
  155. Cyan,
  156. LightGray,
  157. DarkGray,
  158. BrightRed,
  159. BrightGreen,
  160. Yellow,
  161. BrightBlue,
  162. BrightMagenta,
  163. BrightCyan,
  164. White,
  165. __Count,
  166. };
  167. static inline Graphics::Console::Color ansi_color_to_standard_vga_color(ANSIColor color)
  168. {
  169. switch (color) {
  170. case ANSIColor::Black:
  171. return Graphics::Console::Color::Black;
  172. case ANSIColor::Red:
  173. return Graphics::Console::Color::Red;
  174. case ANSIColor::Brown:
  175. return Graphics::Console::Color::Brown;
  176. case ANSIColor::Blue:
  177. return Graphics::Console::Color::Blue;
  178. case ANSIColor::Magenta:
  179. return Graphics::Console::Color::Magenta;
  180. case ANSIColor::Green:
  181. return Graphics::Console::Color::Green;
  182. case ANSIColor::Cyan:
  183. return Graphics::Console::Color::Cyan;
  184. case ANSIColor::LightGray:
  185. return Graphics::Console::Color::LightGray;
  186. case ANSIColor::DarkGray:
  187. return Graphics::Console::Color::DarkGray;
  188. case ANSIColor::BrightRed:
  189. return Graphics::Console::Color::BrightRed;
  190. case ANSIColor::BrightGreen:
  191. return Graphics::Console::Color::BrightGreen;
  192. case ANSIColor::Yellow:
  193. return Graphics::Console::Color::Yellow;
  194. case ANSIColor::BrightBlue:
  195. return Graphics::Console::Color::BrightBlue;
  196. case ANSIColor::BrightMagenta:
  197. return Graphics::Console::Color::BrightMagenta;
  198. case ANSIColor::BrightCyan:
  199. return Graphics::Console::Color::BrightCyan;
  200. case ANSIColor::White:
  201. return Graphics::Console::Color::White;
  202. default:
  203. VERIFY_NOT_REACHED();
  204. }
  205. }
  206. static inline Graphics::Console::Color xterm_to_standard_color(u32 color)
  207. {
  208. for (u8 i = 0; i < (u8)ANSIColor::__Count; i++) {
  209. if (xterm_colors[i] == color)
  210. return (Graphics::Console::Color)ansi_color_to_standard_vga_color((ANSIColor)i);
  211. }
  212. return Graphics::Console::Color::LightGray;
  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. ssize_t VirtualConsole::on_tty_write(const UserOrKernelBuffer& data, ssize_t size)
  226. {
  227. ScopedSpinLock global_lock(ConsoleManagement::the().tty_write_lock());
  228. ScopedSpinLock lock(m_lock);
  229. auto result = data.read_buffered<512>((size_t)size, [&](u8 const* buffer, size_t buffer_bytes) {
  230. for (size_t i = 0; i < buffer_bytes; ++i)
  231. m_console_impl.on_input(buffer[i]);
  232. return buffer_bytes;
  233. });
  234. if (m_active)
  235. flush_dirty_lines();
  236. if (result.is_error())
  237. return result.error();
  238. return (ssize_t)result.value();
  239. }
  240. void VirtualConsole::set_active(bool active)
  241. {
  242. VERIFY(ConsoleManagement::the().m_lock.is_locked());
  243. VERIFY(m_active != active);
  244. m_active = active;
  245. if (active) {
  246. HIDManagement::the().set_client(this);
  247. m_console_impl.m_need_full_flush = true;
  248. flush_dirty_lines();
  249. } else {
  250. HIDManagement::the().set_client(nullptr);
  251. }
  252. }
  253. void VirtualConsole::emit_char(char ch)
  254. {
  255. echo(ch);
  256. }
  257. void VirtualConsole::flush_dirty_lines()
  258. {
  259. VERIFY(GraphicsManagement::is_initialized());
  260. VERIFY(GraphicsManagement::the().console());
  261. for (u16 visual_row = 0; visual_row < rows(); ++visual_row) {
  262. auto& line = m_lines[visual_row];
  263. if (!line.dirty && !m_console_impl.m_need_full_flush)
  264. continue;
  265. for (size_t column = 0; column < columns(); ++column) {
  266. auto& cell = cell_at(column, visual_row);
  267. auto foreground_color = xterm_to_standard_color(cell.attribute.effective_foreground_color());
  268. if (cell.attribute.flags & VT::Attribute::Flags::Bold)
  269. foreground_color = (Graphics::Console::Color)((u8)foreground_color | 0x08);
  270. GraphicsManagement::the().console()->write(column,
  271. visual_row,
  272. ((u8)cell.ch < 128 ? cell.ch : '?'),
  273. xterm_to_standard_color(cell.attribute.effective_background_color()),
  274. foreground_color);
  275. }
  276. line.dirty = false;
  277. }
  278. GraphicsManagement::the().console()->set_cursor(m_console_impl.cursor_column(), m_console_impl.cursor_row());
  279. m_console_impl.m_need_full_flush = false;
  280. }
  281. void VirtualConsole::beep()
  282. {
  283. // TODO
  284. dbgln("Beep!1");
  285. }
  286. void VirtualConsole::set_window_title(const StringView&)
  287. {
  288. // Do nothing.
  289. }
  290. void VirtualConsole::set_window_progress(int, int)
  291. {
  292. // Do nothing.
  293. }
  294. void VirtualConsole::terminal_did_resize(u16 columns, u16 rows)
  295. {
  296. // FIXME: Allocate more Region(s) or deallocate them if needed...
  297. dbgln("VC {}: Resized to {} x {}", index(), columns, rows);
  298. }
  299. void VirtualConsole::terminal_history_changed()
  300. {
  301. // Do nothing, I guess?
  302. }
  303. void VirtualConsole::emit(const u8* data, size_t size)
  304. {
  305. for (size_t i = 0; i < size; i++)
  306. TTY::emit(data[i], true);
  307. }
  308. String VirtualConsole::device_name() const
  309. {
  310. return String::formatted("tty{}", minor());
  311. }
  312. void VirtualConsole::echo(u8 ch)
  313. {
  314. if (should_echo_input()) {
  315. auto buffer = UserOrKernelBuffer::for_kernel_buffer(&ch);
  316. on_tty_write(buffer, 1);
  317. }
  318. }
  319. VirtualConsole::Cell& VirtualConsole::cell_at(size_t x, size_t y)
  320. {
  321. auto* ptr = (VirtualConsole::Cell*)(m_cells->vaddr().as_ptr());
  322. ptr += (y * columns()) + x;
  323. return *ptr;
  324. }
  325. void VirtualConsole::clear()
  326. {
  327. auto* cell = (Cell*)m_cells->vaddr().as_ptr();
  328. for (size_t y = 0; y < rows(); y++) {
  329. m_lines[y].dirty = true;
  330. for (size_t x = 0; x < columns(); x++) {
  331. cell[x].clear();
  332. }
  333. cell += columns();
  334. }
  335. m_console_impl.set_cursor(0, 0);
  336. }
  337. void VirtualConsole::scroll_up()
  338. {
  339. memmove(m_cells->vaddr().as_ptr(), m_cells->vaddr().offset(columns() * sizeof(Cell)).as_ptr(), ((rows() - 1) * columns() * sizeof(Cell)));
  340. clear_line(rows() - 1);
  341. m_console_impl.m_need_full_flush = true;
  342. }
  343. void VirtualConsole::clear_line(size_t y_index)
  344. {
  345. m_lines[y_index].dirty = true;
  346. for (size_t x = 0; x < columns(); x++) {
  347. auto& cell = cell_at(x, y_index);
  348. cell.clear();
  349. }
  350. }
  351. void VirtualConsole::put_character_at(unsigned row, unsigned column, u32 code_point, const VT::Attribute& attribute)
  352. {
  353. VERIFY(row < rows());
  354. VERIFY(column < columns());
  355. auto& line = m_lines[row];
  356. auto& cell = cell_at(column, row);
  357. cell.attribute.foreground_color = attribute.foreground_color;
  358. cell.attribute.background_color = attribute.background_color;
  359. cell.attribute.flags = attribute.flags;
  360. if (code_point > 128)
  361. cell.ch = ' ';
  362. else
  363. cell.ch = code_point;
  364. cell.attribute.flags |= VT::Attribute::Flags::Touched;
  365. line.dirty = true;
  366. }
  367. void VirtualConsole::invalidate_cursor(size_t row)
  368. {
  369. m_lines[row].dirty = true;
  370. }
  371. }