main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "BrowserWindow.h"
  7. #include "EventLoopImplementationQt.h"
  8. #include "Settings.h"
  9. #include "WebContentView.h"
  10. #include <AK/OwnPtr.h>
  11. #include <Ladybird/HelperProcess.h>
  12. #include <Ladybird/Utilities.h>
  13. #include <LibCore/ArgsParser.h>
  14. #include <LibCore/EventLoop.h>
  15. #include <LibCore/Process.h>
  16. #include <LibCore/System.h>
  17. #include <LibGfx/Font/FontDatabase.h>
  18. #include <LibMain/Main.h>
  19. #include <LibWebView/CookieJar.h>
  20. #include <LibWebView/Database.h>
  21. #include <LibWebView/URL.h>
  22. #include <QApplication>
  23. namespace Ladybird {
  24. bool is_using_dark_system_theme(QWidget& widget)
  25. {
  26. // FIXME: Qt does not provide any method to query if the system is using a dark theme. We will have to implement
  27. // platform-specific methods if we wish to have better detection. For now, this inspects if Qt is using a
  28. // dark color for widget backgrounds using Rec. 709 luma coefficients.
  29. // https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients
  30. auto color = widget.palette().color(widget.backgroundRole());
  31. auto luma = 0.2126f * color.redF() + 0.7152f * color.greenF() + 0.0722f * color.blueF();
  32. return luma <= 0.5f;
  33. }
  34. }
  35. static ErrorOr<void> handle_attached_debugger()
  36. {
  37. #ifdef AK_OS_LINUX
  38. // Let's ignore SIGINT if we're being debugged because GDB
  39. // incorrectly forwards the signal to us even when it's set to
  40. // "nopass". See https://sourceware.org/bugzilla/show_bug.cgi?id=9425
  41. // for details.
  42. if (TRY(Core::Process::is_being_debugged())) {
  43. dbgln("Debugger is attached, ignoring SIGINT");
  44. TRY(Core::System::signal(SIGINT, SIG_IGN));
  45. }
  46. #endif
  47. return {};
  48. }
  49. ErrorOr<int> serenity_main(Main::Arguments arguments)
  50. {
  51. QApplication app(arguments.argc, arguments.argv);
  52. Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
  53. Core::EventLoop event_loop;
  54. static_cast<Ladybird::EventLoopImplementationQt&>(event_loop.impl()).set_main_loop();
  55. TRY(handle_attached_debugger());
  56. platform_init();
  57. // NOTE: We only instantiate this to ensure that Gfx::FontDatabase has its default queries initialized.
  58. Gfx::FontDatabase::set_default_font_query("Katica 10 400 0");
  59. Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0");
  60. Vector<StringView> raw_urls;
  61. StringView webdriver_content_ipc_path;
  62. bool enable_callgrind_profiling = false;
  63. bool enable_sql_database = false;
  64. bool use_lagom_networking = false;
  65. bool use_gpu_painting = false;
  66. Core::ArgsParser args_parser;
  67. args_parser.set_general_help("The Ladybird web browser :^)");
  68. args_parser.add_positional_argument(raw_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
  69. args_parser.add_option(webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path", Core::ArgsParser::OptionHideMode::CommandLineAndMarkdown);
  70. args_parser.add_option(enable_callgrind_profiling, "Enable Callgrind profiling", "enable-callgrind-profiling", 'P');
  71. args_parser.add_option(enable_sql_database, "Enable SQL database", "enable-sql-database", 0);
  72. args_parser.add_option(use_lagom_networking, "Enable Lagom servers for networking", "enable-lagom-networking", 0);
  73. args_parser.add_option(use_gpu_painting, "Enable GPU painting", "enable-gpu-painting", 0);
  74. args_parser.parse(arguments);
  75. RefPtr<WebView::Database> database;
  76. if (enable_sql_database) {
  77. auto sql_server_paths = TRY(get_paths_for_helper_process("SQLServer"sv));
  78. database = TRY(WebView::Database::create(move(sql_server_paths)));
  79. }
  80. auto cookie_jar = database ? TRY(WebView::CookieJar::create(*database)) : WebView::CookieJar::create();
  81. Vector<URL> initial_urls;
  82. for (auto const& raw_url : raw_urls) {
  83. if (auto url = WebView::sanitize_url(raw_url); url.has_value())
  84. initial_urls.append(url.release_value());
  85. }
  86. if (initial_urls.is_empty()) {
  87. auto new_tab_page = Ladybird::Settings::the()->new_tab_page();
  88. initial_urls.append(MUST(ak_string_from_qstring(new_tab_page)));
  89. }
  90. Ladybird::BrowserWindow window(initial_urls, cookie_jar, webdriver_content_ipc_path, enable_callgrind_profiling ? WebView::EnableCallgrindProfiling::Yes : WebView::EnableCallgrindProfiling::No, use_lagom_networking ? Ladybird::UseLagomNetworking::Yes : Ladybird::UseLagomNetworking::No, use_gpu_painting ? WebView::EnableGPUPainting::Yes : WebView::EnableGPUPainting::No);
  91. window.setWindowTitle("Ladybird");
  92. if (Ladybird::Settings::the()->is_maximized()) {
  93. window.showMaximized();
  94. } else {
  95. auto last_position = Ladybird::Settings::the()->last_position();
  96. if (last_position.has_value())
  97. window.move(last_position.value());
  98. window.resize(Ladybird::Settings::the()->last_size());
  99. }
  100. window.show();
  101. return event_loop.exec();
  102. }