VirtualConsole.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020 Sergey Bugaev <bugaevc@serenityos.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "VirtualConsole.h"
  28. #include <AK/String.h>
  29. #include <Kernel/Arch/x86/CPU.h>
  30. #include <Kernel/Devices/KeyboardDevice.h>
  31. #include <Kernel/Heap/kmalloc.h>
  32. #include <Kernel/IO.h>
  33. #include <Kernel/StdLib.h>
  34. namespace Kernel {
  35. static u8* s_vga_buffer;
  36. static VirtualConsole* s_consoles[s_max_virtual_consoles];
  37. static int s_active_console;
  38. static RecursiveSpinLock s_lock;
  39. void VirtualConsole::flush_vga_cursor()
  40. {
  41. u16 value = m_current_vga_start_address + (m_terminal.cursor_row() * columns() + m_terminal.cursor_column());
  42. IO::out8(0x3d4, 0x0e);
  43. IO::out8(0x3d5, MSB(value));
  44. IO::out8(0x3d4, 0x0f);
  45. IO::out8(0x3d5, LSB(value));
  46. }
  47. UNMAP_AFTER_INIT void VirtualConsole::initialize()
  48. {
  49. s_vga_buffer = (u8*)0xc00b8000;
  50. s_active_console = -1;
  51. }
  52. void VirtualConsole::set_graphical(bool graphical)
  53. {
  54. if (graphical)
  55. set_vga_start_row(0);
  56. m_graphical = graphical;
  57. }
  58. UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index)
  59. : TTY(4, index)
  60. , m_index(index)
  61. , m_terminal(*this)
  62. {
  63. VERIFY(index < s_max_virtual_consoles);
  64. m_tty_name = String::formatted("/dev/tty{}", m_index);
  65. m_terminal.set_size(80, 25);
  66. s_consoles[index] = this;
  67. }
  68. UNMAP_AFTER_INIT VirtualConsole::~VirtualConsole()
  69. {
  70. VERIFY_NOT_REACHED();
  71. }
  72. void VirtualConsole::switch_to(unsigned index)
  73. {
  74. if ((int)index == s_active_console)
  75. return;
  76. VERIFY(index < s_max_virtual_consoles);
  77. VERIFY(s_consoles[index]);
  78. ScopedSpinLock lock(s_lock);
  79. if (s_active_console != -1) {
  80. auto* active_console = s_consoles[s_active_console];
  81. // We won't know how to switch away from a graphical console until we
  82. // can set the video mode on our own. Just stop anyone from trying for
  83. // now.
  84. if (active_console->is_graphical()) {
  85. dbgln("Cannot switch away from graphical console yet :(");
  86. return;
  87. }
  88. active_console->set_active(false);
  89. }
  90. dbgln("VC: Switch to {} ({})", index, s_consoles[index]);
  91. s_active_console = index;
  92. s_consoles[s_active_console]->set_active(true);
  93. }
  94. void VirtualConsole::set_active(bool active)
  95. {
  96. if (active == m_active)
  97. return;
  98. ScopedSpinLock lock(s_lock);
  99. m_active = active;
  100. if (active) {
  101. set_vga_start_row(0);
  102. KeyboardDevice::the().set_client(this);
  103. m_terminal.m_need_full_flush = true;
  104. flush_dirty_lines();
  105. } else {
  106. KeyboardDevice::the().set_client(nullptr);
  107. }
  108. }
  109. enum class VGAColor : u8 {
  110. Black = 0,
  111. Blue,
  112. Green,
  113. Cyan,
  114. Red,
  115. Magenta,
  116. Brown,
  117. LightGray,
  118. DarkGray,
  119. BrightBlue,
  120. BrightGreen,
  121. BrightCyan,
  122. BrightRed,
  123. BrightMagenta,
  124. Yellow,
  125. White,
  126. };
  127. enum class ANSIColor : u8 {
  128. Black = 0,
  129. Red,
  130. Green,
  131. Brown,
  132. Blue,
  133. Magenta,
  134. Cyan,
  135. LightGray,
  136. DarkGray,
  137. BrightRed,
  138. BrightGreen,
  139. Yellow,
  140. BrightBlue,
  141. BrightMagenta,
  142. BrightCyan,
  143. White,
  144. __Count,
  145. };
  146. static inline VGAColor ansi_color_to_vga(ANSIColor color)
  147. {
  148. switch (color) {
  149. case ANSIColor::Black:
  150. return VGAColor::Black;
  151. case ANSIColor::Red:
  152. return VGAColor::Red;
  153. case ANSIColor::Brown:
  154. return VGAColor::Brown;
  155. case ANSIColor::Blue:
  156. return VGAColor::Blue;
  157. case ANSIColor::Magenta:
  158. return VGAColor::Magenta;
  159. case ANSIColor::Green:
  160. return VGAColor::Green;
  161. case ANSIColor::Cyan:
  162. return VGAColor::Cyan;
  163. case ANSIColor::LightGray:
  164. return VGAColor::LightGray;
  165. case ANSIColor::DarkGray:
  166. return VGAColor::DarkGray;
  167. case ANSIColor::BrightRed:
  168. return VGAColor::BrightRed;
  169. case ANSIColor::BrightGreen:
  170. return VGAColor::BrightGreen;
  171. case ANSIColor::Yellow:
  172. return VGAColor::Yellow;
  173. case ANSIColor::BrightBlue:
  174. return VGAColor::BrightBlue;
  175. case ANSIColor::BrightMagenta:
  176. return VGAColor::BrightMagenta;
  177. case ANSIColor::BrightCyan:
  178. return VGAColor::BrightCyan;
  179. case ANSIColor::White:
  180. return VGAColor::White;
  181. default:
  182. VERIFY_NOT_REACHED();
  183. }
  184. }
  185. static inline u8 xterm_color_to_vga(u32 color)
  186. {
  187. for (u8 i = 0; i < (u8)ANSIColor::__Count; i++) {
  188. if (xterm_colors[i] == color)
  189. return (u8)ansi_color_to_vga((ANSIColor)i);
  190. }
  191. return (u8)VGAColor::LightGray;
  192. }
  193. void VirtualConsole::clear_vga_row(u16 row)
  194. {
  195. u16* linemem = (u16*)&m_current_vga_window[row * 160];
  196. for (u16 i = 0; i < columns(); ++i)
  197. linemem[i] = 0x0720;
  198. }
  199. void VirtualConsole::on_key_pressed(KeyboardDevice::Event event)
  200. {
  201. // Ignore keyboard in graphical mode.
  202. if (m_graphical)
  203. return;
  204. if (!event.is_press())
  205. return;
  206. if (event.key == KeyCode::Key_PageUp && event.flags == Mod_Shift) {
  207. // TODO: scroll up
  208. return;
  209. }
  210. if (event.key == KeyCode::Key_PageDown && event.flags == Mod_Shift) {
  211. // TODO: scroll down
  212. return;
  213. }
  214. Processor::deferred_call_queue([this, event]() {
  215. m_terminal.handle_key_press(event.key, event.code_point, event.flags);
  216. });
  217. }
  218. ssize_t VirtualConsole::on_tty_write(const UserOrKernelBuffer& data, ssize_t size)
  219. {
  220. ScopedSpinLock lock(s_lock);
  221. ssize_t nread = data.read_buffered<512>((size_t)size, [&](const u8* buffer, size_t buffer_bytes) {
  222. for (size_t i = 0; i < buffer_bytes; ++i)
  223. m_terminal.on_input(buffer[i]);
  224. return (ssize_t)buffer_bytes;
  225. });
  226. if (m_active)
  227. flush_dirty_lines();
  228. return nread;
  229. }
  230. void VirtualConsole::set_vga_start_row(u16 row)
  231. {
  232. m_vga_start_row = row;
  233. m_current_vga_start_address = row * columns();
  234. m_current_vga_window = s_vga_buffer + row * 160;
  235. IO::out8(0x3d4, 0x0c);
  236. IO::out8(0x3d5, MSB(m_current_vga_start_address));
  237. IO::out8(0x3d4, 0x0d);
  238. IO::out8(0x3d5, LSB(m_current_vga_start_address));
  239. }
  240. static inline u8 attribute_to_vga(const VT::Attribute& attribute)
  241. {
  242. u8 vga_attr = 0x07;
  243. if (attribute.flags & VT::Attribute::Bold)
  244. vga_attr |= 0x08;
  245. // Background color
  246. vga_attr &= ~0x70;
  247. vga_attr |= xterm_color_to_vga(attribute.effective_background_color()) << 8;
  248. // Foreground color
  249. vga_attr &= ~0x7;
  250. vga_attr |= xterm_color_to_vga(attribute.effective_foreground_color());
  251. return vga_attr;
  252. }
  253. void VirtualConsole::flush_dirty_lines()
  254. {
  255. for (u16 visual_row = 0; visual_row < m_terminal.rows(); ++visual_row) {
  256. auto& line = m_terminal.visible_line(visual_row);
  257. if (!line.is_dirty() && !m_terminal.m_need_full_flush)
  258. continue;
  259. for (size_t column = 0; column < line.length(); ++column) {
  260. u32 code_point = line.code_point(column);
  261. auto attribute = line.attribute_at(column);
  262. u16 vga_index = (visual_row * 160) + (column * 2);
  263. m_current_vga_window[vga_index] = code_point < 128 ? code_point : '?';
  264. m_current_vga_window[vga_index + 1] = attribute_to_vga(attribute);
  265. }
  266. line.set_dirty(false);
  267. }
  268. flush_vga_cursor();
  269. m_terminal.m_need_full_flush = false;
  270. }
  271. void VirtualConsole::beep()
  272. {
  273. // TODO
  274. dbgln("Beep!1");
  275. }
  276. void VirtualConsole::set_window_title(const StringView&)
  277. {
  278. // Do nothing.
  279. }
  280. void VirtualConsole::set_window_progress(int, int)
  281. {
  282. // Do nothing.
  283. }
  284. void VirtualConsole::terminal_did_resize(u16 columns, u16 rows)
  285. {
  286. VERIFY(columns == 80);
  287. VERIFY(rows == 25);
  288. set_size(columns, rows);
  289. }
  290. void VirtualConsole::terminal_history_changed()
  291. {
  292. // Do nothing, I guess?
  293. }
  294. void VirtualConsole::emit(const u8* data, size_t size)
  295. {
  296. for (size_t i = 0; i < size; i++)
  297. TTY::emit(data[i], true);
  298. }
  299. String VirtualConsole::device_name() const
  300. {
  301. return String::formatted("tty{}", minor());
  302. }
  303. void VirtualConsole::echo(u8 ch)
  304. {
  305. if (should_echo_input()) {
  306. auto buffer = UserOrKernelBuffer::for_kernel_buffer(&ch);
  307. on_tty_write(buffer, 1);
  308. }
  309. }
  310. }