main.cpp 5.0 KB

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