main.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "AppletManager.h"
  7. #include "Compositor.h"
  8. #include "EventLoop.h"
  9. #include "Screen.h"
  10. #include "WindowManager.h"
  11. #include <Kernel/API/Graphics.h>
  12. #include <LibCore/ConfigFile.h>
  13. #include <LibCore/DirIterator.h>
  14. #include <LibCore/File.h>
  15. #include <LibCore/System.h>
  16. #include <LibGfx/Palette.h>
  17. #include <LibGfx/SystemTheme.h>
  18. #include <LibMain/Main.h>
  19. #include <signal.h>
  20. #include <string.h>
  21. ErrorOr<int> serenity_main(Main::Arguments)
  22. {
  23. TRY(Core::System::pledge("stdio video thread sendfd recvfd accept rpath wpath cpath unix proc sigaction exec tty"));
  24. TRY(Core::System::unveil("/res", "r"));
  25. TRY(Core::System::unveil("/tmp", "cw"));
  26. TRY(Core::System::unveil("/etc/WindowServer.ini", "rwc"));
  27. TRY(Core::System::unveil("/etc/Keyboard.ini", "r"));
  28. TRY(Core::System::unveil("/dev", "rw"));
  29. TRY(Core::System::unveil("/bin/keymap", "x"));
  30. TRY(Core::System::unveil("/proc/keymap", "r"));
  31. struct sigaction act = {};
  32. act.sa_flags = SA_NOCLDWAIT;
  33. act.sa_handler = SIG_IGN;
  34. TRY(Core::System::sigaction(SIGCHLD, &act, nullptr));
  35. TRY(Core::System::pledge("stdio video thread sendfd recvfd accept rpath wpath cpath unix proc exec tty"));
  36. auto wm_config = TRY(Core::ConfigFile::open("/etc/WindowServer.ini"));
  37. auto theme_name = wm_config->read_entry("Theme", "Name", "Default");
  38. auto theme = Gfx::load_system_theme(String::formatted("/res/themes/{}.ini", theme_name));
  39. VERIFY(theme.is_valid());
  40. Gfx::set_system_theme(theme);
  41. auto palette = Gfx::PaletteImpl::create_with_anonymous_buffer(theme);
  42. auto default_font_query = wm_config->read_entry("Fonts", "Default", "Katica 10 400 0");
  43. auto fixed_width_font_query = wm_config->read_entry("Fonts", "FixedWidth", "Csilla 10 400 0");
  44. auto window_title_font_query = wm_config->read_entry("Fonts", "WindowTitle", "Katica 10 700 0");
  45. Gfx::FontDatabase::set_default_font_query(default_font_query);
  46. Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);
  47. Gfx::FontDatabase::set_window_title_font_query(window_title_font_query);
  48. {
  49. // FIXME: Map switched tty from screens.
  50. // FIXME: Gracefully cleanup the TTY graphics mode.
  51. int tty_fd = TRY(Core::System::open("/dev/tty"sv, O_RDWR));
  52. TRY(Core::System::ioctl(tty_fd, KDSETMODE, KD_GRAPHICS));
  53. TRY(Core::System::close(tty_fd));
  54. }
  55. WindowServer::EventLoop loop;
  56. TRY(Core::System::pledge("stdio video thread sendfd recvfd accept rpath wpath cpath proc exec"));
  57. // First check which screens are explicitly configured
  58. {
  59. AK::HashTable<String> fb_devices_configured;
  60. WindowServer::ScreenLayout screen_layout;
  61. String error_msg;
  62. auto add_unconfigured_display_connector_devices = [&]() -> ErrorOr<void> {
  63. // Enumerate the /dev/gpu/connectorX devices and try to set up any ones we find that we haven't already used
  64. Core::DirIterator di("/dev/gpu", Core::DirIterator::SkipParentAndBaseDir);
  65. while (di.has_next()) {
  66. auto path = di.next_path();
  67. if (!path.starts_with("connector"sv))
  68. continue;
  69. auto full_path = String::formatted("/dev/gpu/{}", path);
  70. if (!Core::File::is_device(full_path))
  71. continue;
  72. auto display_connector_fd = TRY(Core::System::open(full_path, O_RDWR | O_CLOEXEC));
  73. if (int rc = graphics_connector_set_responsible(display_connector_fd); rc != 0)
  74. return Error::from_syscall("graphics_connector_set_responsible"sv, rc);
  75. TRY(Core::System::close(display_connector_fd));
  76. if (fb_devices_configured.find(full_path) != fb_devices_configured.end())
  77. continue;
  78. if (!screen_layout.try_auto_add_display_connector(full_path))
  79. dbgln("Could not auto-add display connector device {} to screen layout", full_path);
  80. }
  81. return {};
  82. };
  83. auto apply_and_generate_generic_screen_layout = [&]() -> ErrorOr<bool> {
  84. screen_layout = {};
  85. fb_devices_configured = {};
  86. TRY(add_unconfigured_display_connector_devices());
  87. if (!WindowServer::Screen::apply_layout(move(screen_layout), error_msg)) {
  88. dbgln("Failed to apply generated fallback screen layout: {}", error_msg);
  89. return false;
  90. }
  91. dbgln("Applied generated fallback screen layout!");
  92. return true;
  93. };
  94. if (screen_layout.load_config(*wm_config, &error_msg)) {
  95. for (auto& screen_info : screen_layout.screens)
  96. if (screen_info.mode == WindowServer::ScreenLayout::Screen::Mode::Device)
  97. fb_devices_configured.set(screen_info.device.value());
  98. TRY(add_unconfigured_display_connector_devices());
  99. if (!WindowServer::Screen::apply_layout(move(screen_layout), error_msg)) {
  100. dbgln("Error applying screen layout: {}", error_msg);
  101. TRY(apply_and_generate_generic_screen_layout());
  102. }
  103. } else {
  104. dbgln("Error loading screen configuration: {}", error_msg);
  105. TRY(apply_and_generate_generic_screen_layout());
  106. }
  107. }
  108. auto& screen_input = WindowServer::ScreenInput::the();
  109. screen_input.set_cursor_location(WindowServer::Screen::main().rect().center());
  110. screen_input.set_acceleration_factor(atof(wm_config->read_entry("Mouse", "AccelerationFactor", "1.0").characters()));
  111. screen_input.set_scroll_step_size(wm_config->read_num_entry("Mouse", "ScrollStepSize", 4));
  112. WindowServer::Compositor::the();
  113. auto wm = WindowServer::WindowManager::construct(*palette);
  114. auto am = WindowServer::AppletManager::construct();
  115. auto mm = WindowServer::MenuManager::construct();
  116. TRY(Core::System::unveil("/tmp", ""));
  117. // NOTE: Because we dynamically need to be able to open new /dev/gpu/connector*
  118. // devices we can't really unveil all of /dev unless we have some
  119. // other mechanism that can hand us file descriptors for these.
  120. TRY(Core::System::unveil(nullptr, nullptr));
  121. dbgln("Entering WindowServer main loop");
  122. loop.exec();
  123. VERIFY_NOT_REACHED();
  124. }