ConsoleManagement.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Singleton.h>
  7. #include <Kernel/CommandLine.h>
  8. #include <Kernel/Debug.h>
  9. #include <Kernel/Graphics/GraphicsManagement.h>
  10. #include <Kernel/Panic.h>
  11. #include <Kernel/Sections.h>
  12. #include <Kernel/TTY/ConsoleManagement.h>
  13. namespace Kernel {
  14. static Singleton<ConsoleManagement> s_the;
  15. void ConsoleManagement::resolution_was_changed()
  16. {
  17. for (auto& console : m_consoles) {
  18. console.refresh_after_resolution_change();
  19. }
  20. }
  21. bool ConsoleManagement::is_initialized()
  22. {
  23. if (!s_the.is_initialized())
  24. return false;
  25. if (s_the->m_consoles.is_empty())
  26. return false;
  27. if (!s_the->m_active_console)
  28. return false;
  29. return true;
  30. }
  31. ConsoleManagement& ConsoleManagement::the()
  32. {
  33. return *s_the;
  34. }
  35. UNMAP_AFTER_INIT ConsoleManagement::ConsoleManagement()
  36. {
  37. }
  38. UNMAP_AFTER_INIT void ConsoleManagement::initialize()
  39. {
  40. for (size_t index = 0; index < s_max_virtual_consoles; index++) {
  41. // FIXME: Better determine the debug TTY we chose...
  42. if (index == 1) {
  43. m_consoles.append(VirtualConsole::create_with_preset_log(index, ConsoleDevice::the().logbuffer()));
  44. continue;
  45. }
  46. m_consoles.append(VirtualConsole::create(index));
  47. }
  48. // Note: By default the active console is the first one.
  49. auto tty_number = kernel_command_line().switch_to_tty();
  50. if (tty_number > m_consoles.size()) {
  51. PANIC("Switch to tty value is invalid: {} ", tty_number);
  52. }
  53. m_active_console = &m_consoles[tty_number];
  54. SpinlockLocker lock(m_lock);
  55. m_active_console->set_active(true);
  56. if (!m_active_console->is_graphical())
  57. m_active_console->clear();
  58. }
  59. void ConsoleManagement::switch_to(unsigned index)
  60. {
  61. SpinlockLocker lock(m_lock);
  62. VERIFY(m_active_console);
  63. VERIFY(index < m_consoles.size());
  64. if (m_active_console->index() == index)
  65. return;
  66. bool was_graphical = m_active_console->is_graphical();
  67. m_active_console->set_active(false);
  68. m_active_console = &m_consoles[index];
  69. dbgln_if(VIRTUAL_CONSOLE_DEBUG, "Console: Switch to {}", index);
  70. // Before setting current console to be "active", switch between graphical mode to "textual" mode
  71. // if needed. This will ensure we clear the screen and also that WindowServer won't print anything
  72. // in between.
  73. if (m_active_console->is_graphical() && !was_graphical) {
  74. GraphicsManagement::the().activate_graphical_mode();
  75. }
  76. if (!m_active_console->is_graphical() && was_graphical) {
  77. GraphicsManagement::the().deactivate_graphical_mode();
  78. }
  79. m_active_console->set_active(true);
  80. }
  81. }