main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/ArgsParser.h>
  7. #include <LibCore/EventLoop.h>
  8. #include <LibCore/Process.h>
  9. #include <LibCore/System.h>
  10. #include <LibGfx/Font/FontDatabase.h>
  11. #include <LibMain/Main.h>
  12. #include <LibWebView/Application.h>
  13. #include <LibWebView/ChromeProcess.h>
  14. #include <LibWebView/EventLoop/EventLoopImplementationQt.h>
  15. #include <LibWebView/HelperProcess.h>
  16. #include <LibWebView/ProcessManager.h>
  17. #include <LibWebView/URL.h>
  18. #include <LibWebView/Utilities.h>
  19. #include <UI/Qt/Application.h>
  20. #include <UI/Qt/BrowserWindow.h>
  21. #include <UI/Qt/Settings.h>
  22. #include <UI/Qt/WebContentView.h>
  23. #if defined(AK_OS_MACOS)
  24. # include <LibWebView/MachPortServer.h>
  25. #endif
  26. namespace Ladybird {
  27. // FIXME: Find a place to put this declaration (and other helper functions).
  28. bool is_using_dark_system_theme(QWidget&);
  29. bool is_using_dark_system_theme(QWidget& widget)
  30. {
  31. // FIXME: Qt does not provide any method to query if the system is using a dark theme. We will have to implement
  32. // platform-specific methods if we wish to have better detection. For now, this inspects if Qt is using a
  33. // dark color for widget backgrounds using Rec. 709 luma coefficients.
  34. // https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients
  35. auto color = widget.palette().color(widget.backgroundRole());
  36. auto luma = 0.2126f * color.redF() + 0.7152f * color.greenF() + 0.0722f * color.blueF();
  37. return luma <= 0.5f;
  38. }
  39. }
  40. static ErrorOr<void> handle_attached_debugger()
  41. {
  42. #ifdef AK_OS_LINUX
  43. // Let's ignore SIGINT if we're being debugged because GDB
  44. // incorrectly forwards the signal to us even when it's set to
  45. // "nopass". See https://sourceware.org/bugzilla/show_bug.cgi?id=9425
  46. // for details.
  47. if (TRY(Core::Process::is_being_debugged())) {
  48. dbgln("Debugger is attached, ignoring SIGINT");
  49. TRY(Core::System::signal(SIGINT, SIG_IGN));
  50. }
  51. #endif
  52. return {};
  53. }
  54. ErrorOr<int> serenity_main(Main::Arguments arguments)
  55. {
  56. AK::set_rich_debug_enabled(true);
  57. Core::EventLoopManager::install(*new WebView::EventLoopManagerQt);
  58. auto app = Ladybird::Application::create(arguments, ak_url_from_qstring(Ladybird::Settings::the()->new_tab_page()));
  59. static_cast<WebView::EventLoopImplementationQt&>(Core::EventLoop::current().impl()).set_main_loop();
  60. TRY(handle_attached_debugger());
  61. WebView::platform_init();
  62. WebView::ChromeProcess chrome_process;
  63. if (app->chrome_options().force_new_process == WebView::ForceNewProcess::No) {
  64. auto disposition = TRY(chrome_process.connect(app->chrome_options().raw_urls, app->chrome_options().new_window));
  65. if (disposition == WebView::ChromeProcess::ProcessDisposition::ExitProcess) {
  66. outln("Opening in existing process");
  67. return 0;
  68. }
  69. }
  70. chrome_process.on_new_tab = [&](auto const& urls) {
  71. auto& window = app->active_window();
  72. for (size_t i = 0; i < urls.size(); ++i) {
  73. window.new_tab_from_url(urls[i], (i == 0) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No);
  74. }
  75. window.show();
  76. window.activateWindow();
  77. window.raise();
  78. };
  79. app->on_open_file = [&](auto file_url) {
  80. auto& window = app->active_window();
  81. window.view().load(file_url);
  82. };
  83. #if defined(AK_OS_MACOS)
  84. auto mach_port_server = make<WebView::MachPortServer>();
  85. WebView::set_mach_server_name(mach_port_server->server_port_name());
  86. mach_port_server->on_receive_child_mach_port = [&app](auto pid, auto port) {
  87. app->set_process_mach_port(pid, move(port));
  88. };
  89. mach_port_server->on_receive_backing_stores = [](WebView::MachPortServer::BackingStoresMessage message) {
  90. if (auto view = WebView::WebContentClient::view_for_pid_and_page_id(message.pid, message.page_id); view.has_value())
  91. view->did_allocate_iosurface_backing_stores(message.front_backing_store_id, move(message.front_backing_store_port), message.back_backing_store_id, move(message.back_backing_store_port));
  92. };
  93. #endif
  94. WebView::copy_default_config_files(Ladybird::Settings::the()->directory());
  95. TRY(app->launch_services());
  96. chrome_process.on_new_window = [&](auto const& urls) {
  97. app->new_window(urls);
  98. };
  99. auto& window = app->new_window(app->chrome_options().urls);
  100. window.setWindowTitle("Ladybird");
  101. if (Ladybird::Settings::the()->is_maximized()) {
  102. window.showMaximized();
  103. } else {
  104. auto last_position = Ladybird::Settings::the()->last_position();
  105. if (last_position.has_value())
  106. window.move(last_position.value());
  107. window.resize(Ladybird::Settings::the()->last_size());
  108. }
  109. window.show();
  110. return app->execute();
  111. }