main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include <QFileOpenEvent>
  24. namespace Ladybird {
  25. bool is_using_dark_system_theme(QWidget& widget)
  26. {
  27. // FIXME: Qt does not provide any method to query if the system is using a dark theme. We will have to implement
  28. // platform-specific methods if we wish to have better detection. For now, this inspects if Qt is using a
  29. // dark color for widget backgrounds using Rec. 709 luma coefficients.
  30. // https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients
  31. auto color = widget.palette().color(widget.backgroundRole());
  32. auto luma = 0.2126f * color.redF() + 0.7152f * color.greenF() + 0.0722f * color.blueF();
  33. return luma <= 0.5f;
  34. }
  35. }
  36. static ErrorOr<void> handle_attached_debugger()
  37. {
  38. #ifdef AK_OS_LINUX
  39. // Let's ignore SIGINT if we're being debugged because GDB
  40. // incorrectly forwards the signal to us even when it's set to
  41. // "nopass". See https://sourceware.org/bugzilla/show_bug.cgi?id=9425
  42. // for details.
  43. if (TRY(Core::Process::is_being_debugged())) {
  44. dbgln("Debugger is attached, ignoring SIGINT");
  45. TRY(Core::System::signal(SIGINT, SIG_IGN));
  46. }
  47. #endif
  48. return {};
  49. }
  50. class LadybirdApplication : public QApplication {
  51. public:
  52. LadybirdApplication(int& argc, char** argv)
  53. : QApplication(argc, argv)
  54. {
  55. }
  56. Function<void(URL)> on_open_file;
  57. bool event(QEvent* event) override
  58. {
  59. switch (event->type()) {
  60. case QEvent::FileOpen: {
  61. if (!on_open_file)
  62. break;
  63. auto const& open_event = *static_cast<QFileOpenEvent const*>(event);
  64. auto file = ak_string_from_qstring(open_event.file());
  65. if (auto file_url = WebView::sanitize_url(file); file_url.has_value())
  66. on_open_file(file_url.release_value());
  67. }
  68. default:
  69. break;
  70. }
  71. return QApplication::event(event);
  72. }
  73. };
  74. ErrorOr<int> serenity_main(Main::Arguments arguments)
  75. {
  76. LadybirdApplication app(arguments.argc, arguments.argv);
  77. Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
  78. Core::EventLoop event_loop;
  79. static_cast<Ladybird::EventLoopImplementationQt&>(event_loop.impl()).set_main_loop();
  80. TRY(handle_attached_debugger());
  81. platform_init();
  82. // NOTE: We only instantiate this to ensure that Gfx::FontDatabase has its default queries initialized.
  83. Gfx::FontDatabase::set_default_font_query("Katica 10 400 0");
  84. Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0");
  85. Vector<StringView> raw_urls;
  86. StringView webdriver_content_ipc_path;
  87. bool enable_callgrind_profiling = false;
  88. bool enable_sql_database = false;
  89. bool use_lagom_networking = false;
  90. bool use_gpu_painting = false;
  91. Core::ArgsParser args_parser;
  92. args_parser.set_general_help("The Ladybird web browser :^)");
  93. args_parser.add_positional_argument(raw_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
  94. args_parser.add_option(webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path", Core::ArgsParser::OptionHideMode::CommandLineAndMarkdown);
  95. args_parser.add_option(enable_callgrind_profiling, "Enable Callgrind profiling", "enable-callgrind-profiling", 'P');
  96. args_parser.add_option(enable_sql_database, "Enable SQL database", "enable-sql-database", 0);
  97. args_parser.add_option(use_lagom_networking, "Enable Lagom servers for networking", "enable-lagom-networking", 0);
  98. args_parser.add_option(use_gpu_painting, "Enable GPU painting", "enable-gpu-painting", 0);
  99. args_parser.parse(arguments);
  100. RefPtr<WebView::Database> database;
  101. if (enable_sql_database) {
  102. auto sql_server_paths = TRY(get_paths_for_helper_process("SQLServer"sv));
  103. database = TRY(WebView::Database::create(move(sql_server_paths)));
  104. }
  105. auto cookie_jar = database ? TRY(WebView::CookieJar::create(*database)) : WebView::CookieJar::create();
  106. Vector<URL> initial_urls;
  107. for (auto const& raw_url : raw_urls) {
  108. if (auto url = WebView::sanitize_url(raw_url); url.has_value())
  109. initial_urls.append(url.release_value());
  110. }
  111. if (initial_urls.is_empty()) {
  112. auto new_tab_page = Ladybird::Settings::the()->new_tab_page();
  113. initial_urls.append(ak_string_from_qstring(new_tab_page));
  114. }
  115. Ladybird::WebContentOptions web_content_options {
  116. .enable_callgrind_profiling = enable_callgrind_profiling ? Ladybird::EnableCallgrindProfiling::Yes : Ladybird::EnableCallgrindProfiling::No,
  117. .enable_gpu_painting = use_gpu_painting ? Ladybird::EnableGPUPainting::Yes : Ladybird::EnableGPUPainting::No,
  118. .use_lagom_networking = use_lagom_networking ? Ladybird::UseLagomNetworking::Yes : Ladybird::UseLagomNetworking::No,
  119. };
  120. Ladybird::BrowserWindow window(initial_urls, cookie_jar, web_content_options, webdriver_content_ipc_path);
  121. window.setWindowTitle("Ladybird");
  122. app.on_open_file = [&](auto file_url) {
  123. window.view().load(file_url);
  124. };
  125. if (Ladybird::Settings::the()->is_maximized()) {
  126. window.showMaximized();
  127. } else {
  128. auto last_position = Ladybird::Settings::the()->last_position();
  129. if (last_position.has_value())
  130. window.move(last_position.value());
  131. window.resize(Ladybird::Settings::the()->last_size());
  132. }
  133. window.show();
  134. return event_loop.exec();
  135. }