main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <LibCore/ConfigFile.h>
  12. #include <LibCore/DirIterator.h>
  13. #include <LibCore/File.h>
  14. #include <LibCore/System.h>
  15. #include <LibGfx/Palette.h>
  16. #include <LibGfx/SystemTheme.h>
  17. #include <LibMain/Main.h>
  18. #include <signal.h>
  19. #include <string.h>
  20. ErrorOr<int> serenity_main(Main::Arguments)
  21. {
  22. TRY(Core::System::pledge("stdio video thread sendfd recvfd accept rpath wpath cpath unix proc sigaction", nullptr));
  23. TRY(Core::System::unveil("/res", "r"));
  24. TRY(Core::System::unveil("/tmp", "cw"));
  25. TRY(Core::System::unveil("/etc/WindowServer.ini", "rwc"));
  26. TRY(Core::System::unveil("/dev", "rw"));
  27. struct sigaction act = {};
  28. act.sa_flags = SA_NOCLDWAIT;
  29. act.sa_handler = SIG_IGN;
  30. TRY(Core::System::sigaction(SIGCHLD, &act, nullptr));
  31. auto wm_config = Core::ConfigFile::open("/etc/WindowServer.ini");
  32. auto theme_name = wm_config->read_entry("Theme", "Name", "Default");
  33. auto theme = Gfx::load_system_theme(String::formatted("/res/themes/{}.ini", theme_name));
  34. VERIFY(theme.is_valid());
  35. Gfx::set_system_theme(theme);
  36. auto palette = Gfx::PaletteImpl::create_with_anonymous_buffer(theme);
  37. auto default_font_query = wm_config->read_entry("Fonts", "Default", "Katica 10 400");
  38. auto fixed_width_font_query = wm_config->read_entry("Fonts", "FixedWidth", "Csilla 10 400");
  39. Gfx::FontDatabase::set_default_font_query(default_font_query);
  40. Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);
  41. WindowServer::EventLoop loop;
  42. TRY(Core::System::pledge("stdio video thread sendfd recvfd accept rpath wpath cpath proc", nullptr));
  43. // First check which screens are explicitly configured
  44. {
  45. AK::HashTable<String> fb_devices_configured;
  46. WindowServer::ScreenLayout screen_layout;
  47. String error_msg;
  48. auto add_unconfigured_devices = [&]() {
  49. // Enumerate the /dev/fbX devices and try to set up any ones we find that we haven't already used
  50. Core::DirIterator di("/dev", Core::DirIterator::SkipParentAndBaseDir);
  51. while (di.has_next()) {
  52. auto path = di.next_path();
  53. if (!path.starts_with("fb"))
  54. continue;
  55. auto full_path = String::formatted("/dev/{}", path);
  56. if (!Core::File::is_device(full_path))
  57. continue;
  58. if (fb_devices_configured.find(full_path) != fb_devices_configured.end())
  59. continue;
  60. if (!screen_layout.try_auto_add_framebuffer(full_path))
  61. dbgln("Could not auto-add framebuffer device {} to screen layout", full_path);
  62. }
  63. };
  64. auto apply_and_generate_generic_screen_layout = [&]() {
  65. screen_layout = {};
  66. fb_devices_configured = {};
  67. add_unconfigured_devices();
  68. if (!WindowServer::Screen::apply_layout(move(screen_layout), error_msg)) {
  69. dbgln("Failed to apply generated fallback screen layout: {}", error_msg);
  70. return false;
  71. }
  72. dbgln("Applied generated fallback screen layout!");
  73. return true;
  74. };
  75. if (screen_layout.load_config(*wm_config, &error_msg)) {
  76. for (auto& screen_info : screen_layout.screens)
  77. fb_devices_configured.set(screen_info.device);
  78. add_unconfigured_devices();
  79. if (!WindowServer::Screen::apply_layout(move(screen_layout), error_msg)) {
  80. dbgln("Error applying screen layout: {}", error_msg);
  81. if (!apply_and_generate_generic_screen_layout())
  82. return 1;
  83. }
  84. } else {
  85. dbgln("Error loading screen configuration: {}", error_msg);
  86. if (!apply_and_generate_generic_screen_layout())
  87. return 1;
  88. }
  89. }
  90. auto& screen_input = WindowServer::ScreenInput::the();
  91. screen_input.set_cursor_location(WindowServer::Screen::main().rect().center());
  92. screen_input.set_acceleration_factor(atof(wm_config->read_entry("Mouse", "AccelerationFactor", "1.0").characters()));
  93. screen_input.set_scroll_step_size(wm_config->read_num_entry("Mouse", "ScrollStepSize", 4));
  94. WindowServer::Compositor::the();
  95. auto wm = WindowServer::WindowManager::construct(*palette);
  96. auto am = WindowServer::AppletManager::construct();
  97. auto mm = WindowServer::MenuManager::construct();
  98. TRY(Core::System::unveil("/tmp", ""));
  99. // NOTE: Because we dynamically need to be able to open new /dev/fb*
  100. // devices we can't really unveil all of /dev unless we have some
  101. // other mechanism that can hand us file descriptors for these.
  102. TRY(Core::System::unveil(nullptr, nullptr));
  103. dbgln("Entering WindowServer main loop");
  104. loop.exec();
  105. VERIFY_NOT_REACHED();
  106. }