VirtualConsole.cpp 8.7 KB

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