MonitorSettingsWidget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2019-2020, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "MonitorSettingsWidget.h"
  8. #include <Applications/DisplaySettings/MonitorSettingsGML.h>
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGUI/Button.h>
  11. #include <LibGUI/ComboBox.h>
  12. #include <LibGUI/ConnectionToWindowServer.h>
  13. #include <LibGUI/ItemListModel.h>
  14. #include <LibGUI/Label.h>
  15. #include <LibGUI/MessageBox.h>
  16. #include <LibGUI/RadioButton.h>
  17. #include <LibGfx/SystemTheme.h>
  18. namespace DisplaySettings {
  19. MonitorSettingsWidget::MonitorSettingsWidget()
  20. {
  21. create_resolution_list();
  22. create_frame();
  23. load_current_settings();
  24. }
  25. void MonitorSettingsWidget::create_resolution_list()
  26. {
  27. // TODO: Find a better way to get the default resolution
  28. m_resolutions.append({ 640, 480 });
  29. m_resolutions.append({ 800, 600 });
  30. m_resolutions.append({ 1024, 768 });
  31. m_resolutions.append({ 1280, 720 });
  32. m_resolutions.append({ 1280, 768 });
  33. m_resolutions.append({ 1280, 960 });
  34. m_resolutions.append({ 1280, 1024 });
  35. m_resolutions.append({ 1360, 768 });
  36. m_resolutions.append({ 1368, 768 });
  37. m_resolutions.append({ 1440, 900 });
  38. m_resolutions.append({ 1600, 900 });
  39. m_resolutions.append({ 1600, 1200 });
  40. m_resolutions.append({ 1920, 1080 });
  41. m_resolutions.append({ 2048, 1152 });
  42. m_resolutions.append({ 2560, 1080 });
  43. m_resolutions.append({ 2560, 1440 });
  44. m_resolutions.append({ 3440, 1440 });
  45. for (auto resolution : m_resolutions) {
  46. // Use Euclid's Algorithm to calculate greatest common factor
  47. i32 a = resolution.width();
  48. i32 b = resolution.height();
  49. i32 gcf = 0;
  50. for (;;) {
  51. i32 r = a % b;
  52. if (r == 0) {
  53. gcf = b;
  54. break;
  55. }
  56. a = b;
  57. b = r;
  58. }
  59. i32 aspect_width = resolution.width() / gcf;
  60. i32 aspect_height = resolution.height() / gcf;
  61. m_resolution_strings.append(String::formatted("{}x{} ({}:{})", resolution.width(), resolution.height(), aspect_width, aspect_height));
  62. }
  63. }
  64. void MonitorSettingsWidget::create_frame()
  65. {
  66. load_from_gml(monitor_settings_window_gml);
  67. m_monitor_widget = *find_descendant_of_type_named<DisplaySettings::MonitorWidget>("monitor_widget");
  68. m_screen_combo = *find_descendant_of_type_named<GUI::ComboBox>("screen_combo");
  69. m_screen_combo->set_only_allow_values_from_model(true);
  70. m_screen_combo->set_model(*GUI::ItemListModel<String>::create(m_screens));
  71. m_screen_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
  72. m_selected_screen_index = index.row();
  73. selected_screen_index_or_resolution_changed();
  74. };
  75. m_resolution_combo = *find_descendant_of_type_named<GUI::ComboBox>("resolution_combo");
  76. m_resolution_combo->set_only_allow_values_from_model(true);
  77. m_resolution_combo->set_model(*GUI::ItemListModel<String>::create(m_resolution_strings));
  78. m_resolution_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
  79. auto& selected_screen = m_screen_layout.screens[m_selected_screen_index];
  80. selected_screen.resolution = m_resolutions.at(index.row());
  81. // Try to auto re-arrange things if there are overlaps or disconnected screens
  82. m_screen_layout.normalize();
  83. selected_screen_index_or_resolution_changed();
  84. };
  85. m_display_scale_radio_1x = *find_descendant_of_type_named<GUI::RadioButton>("scale_1x");
  86. m_display_scale_radio_1x->on_checked = [this](bool checked) {
  87. if (checked) {
  88. auto& selected_screen = m_screen_layout.screens[m_selected_screen_index];
  89. selected_screen.scale_factor = 1;
  90. // Try to auto re-arrange things if there are overlaps or disconnected screens
  91. m_screen_layout.normalize();
  92. m_monitor_widget->set_desktop_scale_factor(1);
  93. m_monitor_widget->update();
  94. }
  95. };
  96. m_display_scale_radio_2x = *find_descendant_of_type_named<GUI::RadioButton>("scale_2x");
  97. m_display_scale_radio_2x->on_checked = [this](bool checked) {
  98. if (checked) {
  99. auto& selected_screen = m_screen_layout.screens[m_selected_screen_index];
  100. selected_screen.scale_factor = 2;
  101. // Try to auto re-arrange things if there are overlaps or disconnected screens
  102. m_screen_layout.normalize();
  103. m_monitor_widget->set_desktop_scale_factor(2);
  104. m_monitor_widget->update();
  105. }
  106. };
  107. m_dpi_label = *find_descendant_of_type_named<GUI::Label>("display_dpi");
  108. }
  109. static String display_name_from_edid(EDID::Parser const& edid)
  110. {
  111. auto manufacturer_name = edid.manufacturer_name();
  112. auto product_name = edid.display_product_name();
  113. auto build_manufacturer_product_name = [&]() {
  114. if (product_name.is_null() || product_name.is_empty())
  115. return manufacturer_name;
  116. return String::formatted("{} {}", manufacturer_name, product_name);
  117. };
  118. if (auto screen_size = edid.screen_size(); screen_size.has_value()) {
  119. auto diagonal_inch = hypot(screen_size.value().horizontal_cm(), screen_size.value().vertical_cm()) / 2.54;
  120. return String::formatted("{} {}\"", build_manufacturer_product_name(), roundf(diagonal_inch));
  121. }
  122. return build_manufacturer_product_name();
  123. }
  124. void MonitorSettingsWidget::load_current_settings()
  125. {
  126. m_screen_layout = GUI::ConnectionToWindowServer::the().get_screen_layout();
  127. m_screens.clear();
  128. m_screen_edids.clear();
  129. size_t virtual_screen_count = 0;
  130. for (size_t i = 0; i < m_screen_layout.screens.size(); i++) {
  131. String screen_display_name;
  132. if (m_screen_layout.screens[i].mode == WindowServer::ScreenLayout::Screen::Mode::Device) {
  133. if (auto edid = EDID::Parser::from_framebuffer_device(m_screen_layout.screens[i].device.value(), 0); !edid.is_error()) { // TODO: multihead
  134. screen_display_name = display_name_from_edid(edid.value());
  135. m_screen_edids.append(edid.release_value());
  136. } else {
  137. dbgln("Error getting EDID from device {}: {}", m_screen_layout.screens[i].device.value(), edid.error());
  138. screen_display_name = m_screen_layout.screens[i].device.value();
  139. m_screen_edids.append({});
  140. }
  141. } else {
  142. dbgln("Frame buffer {} is virtual.", i);
  143. screen_display_name = String::formatted("Virtual screen {}", virtual_screen_count++);
  144. m_screen_edids.append({});
  145. }
  146. if (i == m_screen_layout.main_screen_index)
  147. m_screens.append(String::formatted("{}: {} (main screen)", i + 1, screen_display_name));
  148. else
  149. m_screens.append(String::formatted("{}: {}", i + 1, screen_display_name));
  150. }
  151. m_selected_screen_index = m_screen_layout.main_screen_index;
  152. m_screen_combo->set_selected_index(m_selected_screen_index);
  153. selected_screen_index_or_resolution_changed();
  154. }
  155. void MonitorSettingsWidget::selected_screen_index_or_resolution_changed()
  156. {
  157. auto& screen = m_screen_layout.screens[m_selected_screen_index];
  158. // Let's attempt to find the current resolution based on the screen layout settings
  159. auto index = m_resolutions.find_first_index(screen.resolution).value_or(0);
  160. Gfx::IntSize current_resolution = m_resolutions.at(index);
  161. Optional<unsigned> screen_dpi;
  162. String screen_dpi_tooltip;
  163. if (m_screen_edids[m_selected_screen_index].has_value()) {
  164. auto& edid = m_screen_edids[m_selected_screen_index];
  165. if (auto screen_size = edid.value().screen_size(); screen_size.has_value()) {
  166. auto x_cm = screen_size.value().horizontal_cm();
  167. auto y_cm = screen_size.value().vertical_cm();
  168. auto diagonal_inch = hypot(x_cm, y_cm) / 2.54;
  169. auto diagonal_pixels = hypot(current_resolution.width(), current_resolution.height());
  170. if (diagonal_pixels != 0.0) {
  171. screen_dpi = diagonal_pixels / diagonal_inch;
  172. screen_dpi_tooltip = String::formatted("{} inch display ({}cm x {}cm)", roundf(diagonal_inch), x_cm, y_cm);
  173. }
  174. }
  175. }
  176. if (screen_dpi.has_value()) {
  177. m_dpi_label->set_tooltip(screen_dpi_tooltip);
  178. m_dpi_label->set_text(String::formatted("{} dpi", screen_dpi.value()));
  179. m_dpi_label->set_visible(true);
  180. } else {
  181. m_dpi_label->set_visible(false);
  182. }
  183. if (screen.scale_factor != 1 && screen.scale_factor != 2) {
  184. dbgln("unexpected ScaleFactor {}, setting to 1", screen.scale_factor);
  185. screen.scale_factor = 1;
  186. }
  187. (screen.scale_factor == 1 ? m_display_scale_radio_1x : m_display_scale_radio_2x)->set_checked(true);
  188. m_monitor_widget->set_desktop_scale_factor(screen.scale_factor);
  189. // Select the current selected resolution as it may differ
  190. m_monitor_widget->set_desktop_resolution(current_resolution);
  191. m_resolution_combo->set_selected_index(index);
  192. m_monitor_widget->update();
  193. }
  194. void MonitorSettingsWidget::apply_settings()
  195. {
  196. // Fetch the latest configuration again, in case it has been changed by someone else.
  197. // This isn't technically race free, but if the user automates changing settings we can't help...
  198. auto current_layout = GUI::ConnectionToWindowServer::the().get_screen_layout();
  199. if (m_screen_layout != current_layout) {
  200. auto result = GUI::ConnectionToWindowServer::the().set_screen_layout(m_screen_layout, false);
  201. if (result.success()) {
  202. load_current_settings(); // Refresh
  203. auto seconds_until_revert = 10;
  204. auto box_text = [&seconds_until_revert] {
  205. return String::formatted("Do you want to keep the new settings? They will be reverted after {} {}.",
  206. seconds_until_revert, seconds_until_revert == 1 ? "second" : "seconds");
  207. };
  208. auto box = GUI::MessageBox::construct(window(), box_text(), "Apply new screen layout",
  209. GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
  210. box->set_icon(window()->icon());
  211. // If after 10 seconds the user doesn't close the message box, just close it.
  212. auto revert_timer = Core::Timer::create_repeating(1000, [&] {
  213. seconds_until_revert -= 1;
  214. box->set_text(box_text());
  215. if (seconds_until_revert <= 0) {
  216. box->close();
  217. }
  218. });
  219. revert_timer->start();
  220. // If the user selects "No", closes the window or the window gets closed by the 10 seconds timer, revert the changes.
  221. if (box->exec() == GUI::MessageBox::ExecYes) {
  222. auto save_result = GUI::ConnectionToWindowServer::the().save_screen_layout();
  223. if (!save_result.success()) {
  224. GUI::MessageBox::show(window(), String::formatted("Error saving settings: {}", save_result.error_msg()),
  225. "Unable to save setting", GUI::MessageBox::Type::Error);
  226. }
  227. } else {
  228. auto restore_result = GUI::ConnectionToWindowServer::the().set_screen_layout(current_layout, false);
  229. if (!restore_result.success()) {
  230. GUI::MessageBox::show(window(), String::formatted("Error restoring settings: {}", restore_result.error_msg()),
  231. "Unable to restore setting", GUI::MessageBox::Type::Error);
  232. } else {
  233. load_current_settings();
  234. }
  235. }
  236. } else {
  237. GUI::MessageBox::show(window(), String::formatted("Error setting screen layout: {}", result.error_msg()),
  238. "Unable to apply changes", GUI::MessageBox::Type::Error);
  239. }
  240. }
  241. }
  242. void MonitorSettingsWidget::show_screen_numbers(bool show)
  243. {
  244. if (m_showing_screen_numbers == show)
  245. return;
  246. m_showing_screen_numbers = show;
  247. GUI::ConnectionToWindowServer::the().async_show_screen_numbers(show);
  248. }
  249. void MonitorSettingsWidget::show_event(GUI::ShowEvent&)
  250. {
  251. show_screen_numbers(true);
  252. }
  253. void MonitorSettingsWidget::hide_event(GUI::HideEvent&)
  254. {
  255. show_screen_numbers(false);
  256. }
  257. }