main.cpp 5.4 KB

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