VirtualConsole.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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/i386/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[6];
  37. static int s_active_console;
  38. void VirtualConsole::flush_vga_cursor()
  39. {
  40. u16 value = m_current_vga_start_address + (m_terminal.cursor_row() * columns() + m_terminal.cursor_column());
  41. IO::out8(0x3d4, 0x0e);
  42. IO::out8(0x3d5, MSB(value));
  43. IO::out8(0x3d4, 0x0f);
  44. IO::out8(0x3d5, LSB(value));
  45. }
  46. void VirtualConsole::initialize()
  47. {
  48. s_vga_buffer = (u8*)0xc00b8000;
  49. s_active_console = -1;
  50. }
  51. void VirtualConsole::set_graphical(bool graphical)
  52. {
  53. if (graphical)
  54. set_vga_start_row(0);
  55. m_graphical = graphical;
  56. }
  57. VirtualConsole::VirtualConsole(unsigned index)
  58. : TTY(4, index)
  59. , m_index(index)
  60. , m_terminal(*this)
  61. {
  62. m_tty_name = String::format("/dev/tty%u", m_index);
  63. m_terminal.set_size(80, 25);
  64. s_consoles[index] = this;
  65. }
  66. VirtualConsole::~VirtualConsole()
  67. {
  68. ASSERT_NOT_REACHED();
  69. }
  70. void VirtualConsole::switch_to(unsigned index)
  71. {
  72. if ((int)index == s_active_console)
  73. return;
  74. ASSERT(index < 6);
  75. ASSERT(s_consoles[index]);
  76. InterruptDisabler disabler;
  77. if (s_active_console != -1) {
  78. auto* active_console = s_consoles[s_active_console];
  79. // We won't know how to switch away from a graphical console until we
  80. // can set the video mode on our own. Just stop anyone from trying for
  81. // now.
  82. if (active_console->is_graphical()) {
  83. dbg() << "Cannot switch away from graphical console yet :(";
  84. return;
  85. }
  86. active_console->set_active(false);
  87. }
  88. dbg() << "VC: Switch to " << index << " (" << s_consoles[index] << ")";
  89. s_active_console = index;
  90. s_consoles[s_active_console]->set_active(true);
  91. }
  92. void VirtualConsole::set_active(bool active)
  93. {
  94. if (active == m_active)
  95. return;
  96. InterruptDisabler disabler;
  97. m_active = active;
  98. if (active) {
  99. set_vga_start_row(0);
  100. KeyboardDevice::the().set_client(this);
  101. m_terminal.m_need_full_flush = true;
  102. flush_dirty_lines();
  103. } else {
  104. KeyboardDevice::the().set_client(nullptr);
  105. }
  106. }
  107. enum class VGAColor : u8 {
  108. Black = 0,
  109. Blue,
  110. Green,
  111. Cyan,
  112. Red,
  113. Magenta,
  114. Brown,
  115. LightGray,
  116. DarkGray,
  117. BrightBlue,
  118. BrightGreen,
  119. BrightCyan,
  120. BrightRed,
  121. BrightMagenta,
  122. Yellow,
  123. White,
  124. };
  125. enum class ANSIColor : u8 {
  126. Black = 0,
  127. Red,
  128. Green,
  129. Brown,
  130. Blue,
  131. Magenta,
  132. Cyan,
  133. LightGray,
  134. DarkGray,
  135. BrightRed,
  136. BrightGreen,
  137. Yellow,
  138. BrightBlue,
  139. BrightMagenta,
  140. BrightCyan,
  141. White,
  142. __Count,
  143. };
  144. static inline VGAColor ansi_color_to_vga(ANSIColor color)
  145. {
  146. switch (color) {
  147. case ANSIColor::Black:
  148. return VGAColor::Black;
  149. case ANSIColor::Red:
  150. return VGAColor::Red;
  151. case ANSIColor::Brown:
  152. return VGAColor::Brown;
  153. case ANSIColor::Blue:
  154. return VGAColor::Blue;
  155. case ANSIColor::Magenta:
  156. return VGAColor::Magenta;
  157. case ANSIColor::Green:
  158. return VGAColor::Green;
  159. case ANSIColor::Cyan:
  160. return VGAColor::Cyan;
  161. case ANSIColor::LightGray:
  162. return VGAColor::LightGray;
  163. case ANSIColor::DarkGray:
  164. return VGAColor::DarkGray;
  165. case ANSIColor::BrightRed:
  166. return VGAColor::BrightRed;
  167. case ANSIColor::BrightGreen:
  168. return VGAColor::BrightGreen;
  169. case ANSIColor::Yellow:
  170. return VGAColor::Yellow;
  171. case ANSIColor::BrightBlue:
  172. return VGAColor::BrightBlue;
  173. case ANSIColor::BrightMagenta:
  174. return VGAColor::BrightMagenta;
  175. case ANSIColor::BrightCyan:
  176. return VGAColor::BrightCyan;
  177. case ANSIColor::White:
  178. return VGAColor::White;
  179. default:
  180. ASSERT_NOT_REACHED();
  181. }
  182. }
  183. static inline u8 xterm_color_to_vga(u32 color)
  184. {
  185. for (u8 i = 0; i < (u8)ANSIColor::__Count; i++) {
  186. if (xterm_colors[i] == color)
  187. return (u8)ansi_color_to_vga((ANSIColor)i);
  188. }
  189. return (u8)VGAColor::LightGray;
  190. }
  191. void VirtualConsole::clear_vga_row(u16 row)
  192. {
  193. u16* linemem = (u16*)&m_current_vga_window[row * 160];
  194. for (u16 i = 0; i < columns(); ++i)
  195. linemem[i] = 0x0720;
  196. }
  197. void VirtualConsole::on_key_pressed(KeyboardDevice::Event event)
  198. {
  199. // Ignore keyboard in graphical mode.
  200. if (m_graphical)
  201. return;
  202. if (!event.is_press())
  203. return;
  204. if (event.key == KeyCode::Key_PageUp && event.flags == Mod_Shift) {
  205. // TODO: scroll up
  206. return;
  207. }
  208. if (event.key == KeyCode::Key_PageDown && event.flags == Mod_Shift) {
  209. // TODO: scroll down
  210. return;
  211. }
  212. m_terminal.handle_key_press(event.key, event.character, event.flags);
  213. }
  214. ssize_t VirtualConsole::on_tty_write(const u8* data, ssize_t size)
  215. {
  216. InterruptDisabler disabler;
  217. for (ssize_t i = 0; i < size; ++i)
  218. m_terminal.on_input(data[i]);
  219. if (m_active)
  220. flush_dirty_lines();
  221. return size;
  222. }
  223. void VirtualConsole::set_vga_start_row(u16 row)
  224. {
  225. m_vga_start_row = row;
  226. m_current_vga_start_address = row * columns();
  227. m_current_vga_window = s_vga_buffer + row * 160;
  228. IO::out8(0x3d4, 0x0c);
  229. IO::out8(0x3d5, MSB(m_current_vga_start_address));
  230. IO::out8(0x3d4, 0x0d);
  231. IO::out8(0x3d5, LSB(m_current_vga_start_address));
  232. }
  233. static inline u8 attribute_to_vga(const VT::Attribute& attribute)
  234. {
  235. u8 vga_attr = 0x07;
  236. if (attribute.flags & VT::Attribute::Bold)
  237. vga_attr |= 0x08;
  238. // Background color
  239. vga_attr &= ~0x70;
  240. vga_attr |= xterm_color_to_vga(attribute.background_color) << 8;
  241. // Foreground color
  242. vga_attr &= ~0x7;
  243. vga_attr |= xterm_color_to_vga(attribute.foreground_color);
  244. return vga_attr;
  245. }
  246. void VirtualConsole::flush_dirty_lines()
  247. {
  248. for (u16 visual_row = 0; visual_row < m_terminal.rows(); ++visual_row) {
  249. auto& line = m_terminal.visible_line(visual_row);
  250. if (!line.is_dirty() && !m_terminal.m_need_full_flush)
  251. continue;
  252. for (size_t column = 0; column < line.length(); ++column) {
  253. u32 codepoint = line.codepoint(column);
  254. auto attribute = line.attributes()[column];
  255. u16 vga_index = (visual_row * 160) + (column * 2);
  256. m_current_vga_window[vga_index] = codepoint < 128 ? codepoint : '?';
  257. m_current_vga_window[vga_index + 1] = attribute_to_vga(attribute);
  258. }
  259. line.set_dirty(false);
  260. }
  261. flush_vga_cursor();
  262. m_terminal.m_need_full_flush = false;
  263. }
  264. void VirtualConsole::beep()
  265. {
  266. // TODO
  267. dbg() << "Beep!1";
  268. }
  269. void VirtualConsole::set_window_title(const StringView&)
  270. {
  271. // Do nothing.
  272. }
  273. void VirtualConsole::set_window_progress(int, int)
  274. {
  275. // Do nothing.
  276. }
  277. void VirtualConsole::terminal_did_resize(u16 columns, u16 rows)
  278. {
  279. ASSERT(columns == 80);
  280. ASSERT(rows == 25);
  281. set_size(columns, rows);
  282. }
  283. void VirtualConsole::terminal_history_changed()
  284. {
  285. // Do nothing, I guess?
  286. }
  287. void VirtualConsole::emit(const u8* data, size_t size)
  288. {
  289. for (size_t i = 0; i < size; i++)
  290. TTY::emit(data[i]);
  291. }
  292. void VirtualConsole::echo(u8 ch)
  293. {
  294. if (should_echo_input()) {
  295. on_tty_write(&ch, 1);
  296. }
  297. }
  298. }