main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. 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. ErrorOr<int> serenity_main(Main::Arguments arguments)
  51. {
  52. QApplication app(arguments.argc, arguments.argv);
  53. Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
  54. Core::EventLoop event_loop;
  55. static_cast<Ladybird::EventLoopImplementationQt&>(event_loop.impl()).set_main_loop();
  56. TRY(handle_attached_debugger());
  57. platform_init();
  58. // NOTE: We only instantiate this to ensure that Gfx::FontDatabase has its default queries initialized.
  59. Gfx::FontDatabase::set_default_font_query("Katica 10 400 0");
  60. Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0");
  61. StringView raw_url;
  62. StringView webdriver_content_ipc_path;
  63. bool enable_callgrind_profiling = false;
  64. bool enable_sql_database = false;
  65. bool use_lagom_networking = false;
  66. Core::ArgsParser args_parser;
  67. args_parser.set_general_help("The Ladybird web browser :^)");
  68. args_parser.add_positional_argument(raw_url, "URL 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");
  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.parse(arguments);
  74. auto get_formatted_url = [&](StringView const& raw_url) -> ErrorOr<URL> {
  75. URL url = raw_url;
  76. if (FileSystem::exists(raw_url))
  77. url = URL::create_with_file_scheme(TRY(FileSystem::real_path(raw_url)).to_deprecated_string());
  78. else if (!url.is_valid())
  79. url = DeprecatedString::formatted("https://{}", raw_url);
  80. return url;
  81. };
  82. RefPtr<Browser::Database> database;
  83. if (enable_sql_database) {
  84. auto sql_server_paths = TRY(get_paths_for_helper_process("SQLServer"sv));
  85. auto sql_client = TRY(SQL::SQLClient::launch_server_and_create_client(move(sql_server_paths)));
  86. database = TRY(Browser::Database::create(move(sql_client)));
  87. }
  88. auto cookie_jar = database ? TRY(Browser::CookieJar::create(*database)) : Browser::CookieJar::create();
  89. Optional<URL> initial_url;
  90. if (auto url = TRY(get_formatted_url(raw_url)); url.is_valid())
  91. initial_url = move(url);
  92. 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);
  93. window.setWindowTitle("Ladybird");
  94. if (Ladybird::Settings::the()->is_maximized()) {
  95. window.showMaximized();
  96. } else {
  97. auto last_position = Ladybird::Settings::the()->last_position();
  98. if (last_position.has_value())
  99. window.move(last_position.value());
  100. window.resize(Ladybird::Settings::the()->last_size());
  101. }
  102. window.show();
  103. return event_loop.exec();
  104. }