From ccb439efb088c990b41a6ceb5b1b330c8c27a1aa Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Sun, 8 Nov 2020 07:17:12 -0500 Subject: applets: Remove the previous web browser applet implementation --- src/yuzu/applets/web_browser.h | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) (limited to 'src/yuzu/applets/web_browser.h') diff --git a/src/yuzu/applets/web_browser.h b/src/yuzu/applets/web_browser.h index f801846cf..af053ace7 100644 --- a/src/yuzu/applets/web_browser.h +++ b/src/yuzu/applets/web_browser.h @@ -1,10 +1,9 @@ -// Copyright 2018 yuzu Emulator Project +// Copyright 2020 yuzu Emulator Project // Licensed under GPLv2 or any later version // Refer to the license.txt file included. #pragma once -#include #include #ifdef YUZU_USE_QT_WEB_ENGINE @@ -15,38 +14,10 @@ class GMainWindow; -#ifdef YUZU_USE_QT_WEB_ENGINE - -QString GetNXShimInjectionScript(); - -class NXInputWebEngineView : public QWebEngineView { -public: - explicit NXInputWebEngineView(QWidget* parent = nullptr); - -protected: - void keyPressEvent(QKeyEvent* event) override; - void keyReleaseEvent(QKeyEvent* event) override; -}; - -#endif - class QtWebBrowser final : public QObject, public Core::Frontend::WebBrowserApplet { Q_OBJECT public: explicit QtWebBrowser(GMainWindow& main_window); ~QtWebBrowser() override; - - void OpenPageLocal(std::string_view url, std::function unpack_romfs_callback_, - std::function finished_callback_) override; - -signals: - void MainWindowOpenPage(std::string_view filename, std::string_view additional_args) const; - -private: - void MainWindowUnpackRomFS(); - void MainWindowFinishedBrowsing(); - - std::function unpack_romfs_callback; - std::function finished_callback; }; -- cgit v1.2.3 From 93cb7838539075f0d94cb08a65a25b0c05589d7d Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Mon, 30 Nov 2020 08:31:26 -0500 Subject: applets/web: Implement the Qt web browser applet frontend --- src/yuzu/applets/web_browser.cpp | 333 ++++++++++++++++++++++++++++++++++++++- src/yuzu/applets/web_browser.h | 155 +++++++++++++++++- src/yuzu/main.cpp | 110 ++++++++++++- src/yuzu/main.h | 7 + 4 files changed, 600 insertions(+), 5 deletions(-) (limited to 'src/yuzu/applets/web_browser.h') diff --git a/src/yuzu/applets/web_browser.cpp b/src/yuzu/applets/web_browser.cpp index 92b53fed0..26b9df51a 100644 --- a/src/yuzu/applets/web_browser.cpp +++ b/src/yuzu/applets/web_browser.cpp @@ -2,10 +2,339 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include "core/hle/lock.h" +#ifdef YUZU_USE_QT_WEB_ENGINE +#include + +#include +#include +#include +#include +#include +#endif + +#include "common/file_util.h" +#include "core/core.h" +#include "core/frontend/input_interpreter.h" #include "yuzu/applets/web_browser.h" +#include "yuzu/applets/web_browser_scripts.h" #include "yuzu/main.h" +#include "yuzu/util/url_request_interceptor.h" + +#ifdef YUZU_USE_QT_WEB_ENGINE + +namespace { + +constexpr int HIDButtonToKey(HIDButton button) { + switch (button) { + case HIDButton::DLeft: + case HIDButton::LStickLeft: + return Qt::Key_Left; + case HIDButton::DUp: + case HIDButton::LStickUp: + return Qt::Key_Up; + case HIDButton::DRight: + case HIDButton::LStickRight: + return Qt::Key_Right; + case HIDButton::DDown: + case HIDButton::LStickDown: + return Qt::Key_Down; + default: + return 0; + } +} + +} // Anonymous namespace + +QtNXWebEngineView::QtNXWebEngineView(QWidget* parent, Core::System& system) + : QWebEngineView(parent), url_interceptor(std::make_unique()), + input_interpreter(std::make_unique(system)) { + QWebEngineScript nx_font_css; + QWebEngineScript load_nx_font; + QWebEngineScript gamepad; + QWebEngineScript window_nx; + + const QString fonts_dir = QString::fromStdString(Common::FS::SanitizePath( + fmt::format("{}/fonts", Common::FS::GetUserPath(Common::FS::UserPath::CacheDir)))); + + nx_font_css.setName(QStringLiteral("nx_font_css.js")); + load_nx_font.setName(QStringLiteral("load_nx_font.js")); + gamepad.setName(QStringLiteral("gamepad_script.js")); + window_nx.setName(QStringLiteral("window_nx_script.js")); + + nx_font_css.setSourceCode( + QString::fromStdString(NX_FONT_CSS) + .arg(fonts_dir + QStringLiteral("/FontStandard.ttf")) + .arg(fonts_dir + QStringLiteral("/FontChineseSimplified.ttf")) + .arg(fonts_dir + QStringLiteral("/FontExtendedChineseSimplified.ttf")) + .arg(fonts_dir + QStringLiteral("/FontChineseTraditional.ttf")) + .arg(fonts_dir + QStringLiteral("/FontKorean.ttf")) + .arg(fonts_dir + QStringLiteral("/FontNintendoExtended.ttf")) + .arg(fonts_dir + QStringLiteral("/FontNintendoExtended2.ttf"))); + load_nx_font.setSourceCode(QString::fromStdString(LOAD_NX_FONT)); + gamepad.setSourceCode(QString::fromStdString(GAMEPAD_SCRIPT)); + window_nx.setSourceCode(QString::fromStdString(WINDOW_NX_SCRIPT)); + + nx_font_css.setInjectionPoint(QWebEngineScript::DocumentReady); + load_nx_font.setInjectionPoint(QWebEngineScript::Deferred); + gamepad.setInjectionPoint(QWebEngineScript::DocumentCreation); + window_nx.setInjectionPoint(QWebEngineScript::DocumentCreation); + + nx_font_css.setWorldId(QWebEngineScript::MainWorld); + load_nx_font.setWorldId(QWebEngineScript::MainWorld); + gamepad.setWorldId(QWebEngineScript::MainWorld); + window_nx.setWorldId(QWebEngineScript::MainWorld); + + nx_font_css.setRunsOnSubFrames(true); + load_nx_font.setRunsOnSubFrames(true); + gamepad.setRunsOnSubFrames(true); + window_nx.setRunsOnSubFrames(true); + + auto* default_profile = QWebEngineProfile::defaultProfile(); + + default_profile->scripts()->insert(nx_font_css); + default_profile->scripts()->insert(load_nx_font); + default_profile->scripts()->insert(gamepad); + default_profile->scripts()->insert(window_nx); + + default_profile->setRequestInterceptor(url_interceptor.get()); + + auto* global_settings = QWebEngineSettings::globalSettings(); + + global_settings->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true); + global_settings->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true); + global_settings->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, true); + global_settings->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, true); + global_settings->setAttribute(QWebEngineSettings::AllowWindowActivationFromJavaScript, true); + global_settings->setAttribute(QWebEngineSettings::ShowScrollBars, false); + + connect( + url_interceptor.get(), &UrlRequestInterceptor::FrameChanged, url_interceptor.get(), + [this] { + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + page()->runJavaScript(QString::fromStdString(LOAD_NX_FONT)); + }, + Qt::QueuedConnection); + + connect( + page(), &QWebEnginePage::windowCloseRequested, page(), + [this] { + if (page()->url() == url_interceptor->GetRequestedURL()) { + SetFinished(true); + SetExitReason(WebExitReason::WindowClosed); + } + }, + Qt::QueuedConnection); +} + +QtNXWebEngineView::~QtNXWebEngineView() { + SetFinished(true); + StopInputThread(); +} + +void QtNXWebEngineView::LoadLocalWebPage(std::string_view main_url, + std::string_view additional_args) { + SetUserAgent(UserAgent::WebApplet); + SetFinished(false); + SetExitReason(WebExitReason::EndButtonPressed); + SetLastURL("http://localhost/"); + StartInputThread(); + + load(QUrl(QUrl::fromLocalFile(QString::fromStdString(std::string(main_url))).toString() + + QString::fromStdString(std::string(additional_args)))); +} + +void QtNXWebEngineView::SetUserAgent(UserAgent user_agent) { + const QString user_agent_str = [user_agent] { + switch (user_agent) { + case UserAgent::WebApplet: + default: + return QStringLiteral("WebApplet"); + case UserAgent::ShopN: + return QStringLiteral("ShopN"); + case UserAgent::LoginApplet: + return QStringLiteral("LoginApplet"); + case UserAgent::ShareApplet: + return QStringLiteral("ShareApplet"); + case UserAgent::LobbyApplet: + return QStringLiteral("LobbyApplet"); + case UserAgent::WifiWebAuthApplet: + return QStringLiteral("WifiWebAuthApplet"); + } + }(); + + QWebEngineProfile::defaultProfile()->setHttpUserAgent( + QStringLiteral("Mozilla/5.0 (Nintendo Switch; %1) AppleWebKit/606.4 " + "(KHTML, like Gecko) NF/6.0.1.15.4 NintendoBrowser/5.1.0.20389") + .arg(user_agent_str)); +} + +bool QtNXWebEngineView::IsFinished() const { + return finished; +} + +void QtNXWebEngineView::SetFinished(bool finished_) { + finished = finished_; +} + +WebExitReason QtNXWebEngineView::GetExitReason() const { + return exit_reason; +} + +void QtNXWebEngineView::SetExitReason(WebExitReason exit_reason_) { + exit_reason = exit_reason_; +} -QtWebBrowser::QtWebBrowser(GMainWindow& main_window) {} +const std::string& QtNXWebEngineView::GetLastURL() const { + return last_url; +} + +void QtNXWebEngineView::SetLastURL(std::string last_url_) { + last_url = std::move(last_url_); +} + +QString QtNXWebEngineView::GetCurrentURL() const { + return url_interceptor->GetRequestedURL().toString(); +} + +void QtNXWebEngineView::hide() { + SetFinished(true); + StopInputThread(); + + QWidget::hide(); +} + +template +void QtNXWebEngineView::HandleWindowFooterButtonPressedOnce() { + const auto f = [this](HIDButton button) { + if (input_interpreter->IsButtonPressedOnce(button)) { + page()->runJavaScript( + QStringLiteral("yuzu_key_callbacks[%1] == null;").arg(static_cast(button)), + [&](const QVariant& variant) { + if (variant.toBool()) { + switch (button) { + case HIDButton::A: + SendMultipleKeyPressEvents(); + break; + case HIDButton::B: + SendKeyPressEvent(Qt::Key_B); + break; + case HIDButton::X: + SendKeyPressEvent(Qt::Key_X); + break; + case HIDButton::Y: + SendKeyPressEvent(Qt::Key_Y); + break; + default: + break; + } + } + }); + + page()->runJavaScript( + QStringLiteral("if (yuzu_key_callbacks[%1] != null) { yuzu_key_callbacks[%1](); }") + .arg(static_cast(button))); + } + }; + + (f(T), ...); +} + +template +void QtNXWebEngineView::HandleWindowKeyButtonPressedOnce() { + const auto f = [this](HIDButton button) { + if (input_interpreter->IsButtonPressedOnce(button)) { + SendKeyPressEvent(HIDButtonToKey(button)); + } + }; + + (f(T), ...); +} + +template +void QtNXWebEngineView::HandleWindowKeyButtonHold() { + const auto f = [this](HIDButton button) { + if (input_interpreter->IsButtonHeld(button)) { + SendKeyPressEvent(HIDButtonToKey(button)); + } + }; + + (f(T), ...); +} + +void QtNXWebEngineView::SendKeyPressEvent(int key) { + if (key == 0) { + return; + } + + QCoreApplication::postEvent(focusProxy(), + new QKeyEvent(QKeyEvent::KeyPress, key, Qt::NoModifier)); + QCoreApplication::postEvent(focusProxy(), + new QKeyEvent(QKeyEvent::KeyRelease, key, Qt::NoModifier)); +} + +void QtNXWebEngineView::StartInputThread() { + if (input_thread_running) { + return; + } + + input_thread_running = true; + input_thread = std::thread(&QtNXWebEngineView::InputThread, this); +} + +void QtNXWebEngineView::StopInputThread() { + input_thread_running = false; + if (input_thread.joinable()) { + input_thread.join(); + } +} + +void QtNXWebEngineView::InputThread() { + // Wait for 1 second before allowing any inputs to be processed. + std::this_thread::sleep_for(std::chrono::seconds(1)); + + while (input_thread_running) { + input_interpreter->PollInput(); + + HandleWindowFooterButtonPressedOnce(); + + HandleWindowKeyButtonPressedOnce(); + + HandleWindowKeyButtonHold(); + + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + } +} + +#endif + +QtWebBrowser::QtWebBrowser(GMainWindow& main_window) { + connect(this, &QtWebBrowser::MainWindowOpenLocalWebPage, &main_window, + &GMainWindow::WebBrowserOpenLocalWebPage, Qt::QueuedConnection); + connect(&main_window, &GMainWindow::WebBrowserClosed, this, + &QtWebBrowser::MainWindowWebBrowserClosed, Qt::QueuedConnection); +} QtWebBrowser::~QtWebBrowser() = default; + +void QtWebBrowser::OpenLocalWebPage( + std::string_view local_url, std::function callback) const { + this->callback = std::move(callback); + + const auto index = local_url.find('?'); + + if (index == std::string::npos) { + emit MainWindowOpenLocalWebPage(local_url, ""); + } else { + emit MainWindowOpenLocalWebPage(local_url.substr(0, index), local_url.substr(index)); + } +} + +void QtWebBrowser::MainWindowWebBrowserClosed(WebExitReason exit_reason, std::string last_url) { + callback(exit_reason, last_url); +} diff --git a/src/yuzu/applets/web_browser.h b/src/yuzu/applets/web_browser.h index af053ace7..74f2b49d2 100644 --- a/src/yuzu/applets/web_browser.h +++ b/src/yuzu/applets/web_browser.h @@ -4,6 +4,10 @@ #pragma once +#include +#include +#include + #include #ifdef YUZU_USE_QT_WEB_ENGINE @@ -12,12 +16,161 @@ #include "core/frontend/applets/web_browser.h" +enum class HIDButton : u8; + +class InputInterpreter; class GMainWindow; +class UrlRequestInterceptor; + +namespace Core { +class System; +} + +#ifdef YUZU_USE_QT_WEB_ENGINE + +enum class UserAgent { + WebApplet, + ShopN, + LoginApplet, + ShareApplet, + LobbyApplet, + WifiWebAuthApplet, +}; + +class QtNXWebEngineView : public QWebEngineView { + Q_OBJECT + +public: + explicit QtNXWebEngineView(QWidget* parent, Core::System& system); + ~QtNXWebEngineView() override; + + /** + * Loads a HTML document that exists locally. Cannot be used to load external websites. + * + * @param main_url The url to the file. + * @param additional_args Additional arguments appended to the main url. + */ + void LoadLocalWebPage(std::string_view main_url, std::string_view additional_args); + + /** + * Sets the background color of the web page. + * + * @param color The color to set. + */ + void SetBackgroundColor(QColor color); + + /** + * Sets the user agent of the web browser. + * + * @param user_agent The user agent enum. + */ + void SetUserAgent(UserAgent user_agent); + + [[nodiscard]] bool IsFinished() const; + void SetFinished(bool finished_); + + [[nodiscard]] WebExitReason GetExitReason() const; + void SetExitReason(WebExitReason exit_reason_); + + [[nodiscard]] const std::string& GetLastURL() const; + void SetLastURL(std::string last_url_); + + /** + * This gets the current URL that has been requested by the webpage. + * This only applies to the main frame. Sub frames and other resources are ignored. + * + * @return Currently requested URL + */ + [[nodiscard]] QString GetCurrentURL() const; + +public slots: + void hide(); + +private: + /** + * Handles button presses to execute functions assigned in yuzu_key_callbacks. + * yuzu_key_callbacks contains specialized functions for the buttons in the window footer + * that can be overriden by games to achieve desired functionality. + * + * @tparam HIDButton The list of buttons contained in yuzu_key_callbacks + */ + template + void HandleWindowFooterButtonPressedOnce(); + + /** + * Handles button presses and converts them into keyboard input. + * This should only be used to convert D-Pad or Analog Stick input into arrow keys. + * + * @tparam HIDButton The list of buttons that can be converted into keyboard input. + */ + template + void HandleWindowKeyButtonPressedOnce(); + + /** + * Handles button holds and converts them into keyboard input. + * This should only be used to convert D-Pad or Analog Stick input into arrow keys. + * + * @tparam HIDButton The list of buttons that can be converted into keyboard input. + */ + template + void HandleWindowKeyButtonHold(); + + /** + * Sends a key press event to QWebEngineView. + * + * @param key Qt key code. + */ + void SendKeyPressEvent(int key); + + /** + * Sends multiple key press events to QWebEngineView. + * + * @tparam int Qt key code. + */ + template + void SendMultipleKeyPressEvents() { + (SendKeyPressEvent(T), ...); + } + + void StartInputThread(); + void StopInputThread(); + + /// The thread where input is being polled and processed. + void InputThread(); + + std::unique_ptr url_interceptor; + + std::unique_ptr input_interpreter; + + std::thread input_thread; + + std::atomic input_thread_running{}; + + std::atomic finished{}; + + WebExitReason exit_reason{WebExitReason::EndButtonPressed}; + + std::string last_url{"http://localhost/"}; +}; + +#endif class QtWebBrowser final : public QObject, public Core::Frontend::WebBrowserApplet { Q_OBJECT public: - explicit QtWebBrowser(GMainWindow& main_window); + explicit QtWebBrowser(GMainWindow& parent); ~QtWebBrowser() override; + + void OpenLocalWebPage(std::string_view local_url, + std::function callback) const override; + +signals: + void MainWindowOpenLocalWebPage(std::string_view main_url, + std::string_view additional_args) const; + +private: + void MainWindowWebBrowserClosed(WebExitReason exit_reason, std::string last_url); + + mutable std::function callback; }; diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 7d4bba854..bab76db1e 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -28,8 +28,6 @@ #include "core/hle/service/am/applet_ae.h" #include "core/hle/service/am/applet_oe.h" #include "core/hle/service/am/applets/applets.h" -#include "core/hle/service/hid/controllers/npad.h" -#include "core/hle/service/hid/hid.h" // These are wrappers to avoid the calls to CreateDirectory and CreateFile because of the Windows // defines. @@ -182,6 +180,30 @@ static void InitializeLogging() { #endif } +static void RemoveCachedContents() { + const auto offline_fonts = Common::FS::SanitizePath( + fmt::format("{}/fonts", Common::FS::GetUserPath(Common::FS::UserPath::CacheDir)), + Common::FS::DirectorySeparator::PlatformDefault); + + const auto offline_manual = Common::FS::SanitizePath( + fmt::format("{}/offline_web_applet_manual", + Common::FS::GetUserPath(Common::FS::UserPath::CacheDir)), + Common::FS::DirectorySeparator::PlatformDefault); + const auto offline_legal_information = Common::FS::SanitizePath( + fmt::format("{}/offline_web_applet_legal_information", + Common::FS::GetUserPath(Common::FS::UserPath::CacheDir)), + Common::FS::DirectorySeparator::PlatformDefault); + const auto offline_system_data = Common::FS::SanitizePath( + fmt::format("{}/offline_web_applet_system_data", + Common::FS::GetUserPath(Common::FS::UserPath::CacheDir)), + Common::FS::DirectorySeparator::PlatformDefault); + + Common::FS::DeleteDirRecursively(offline_fonts); + Common::FS::DeleteDirRecursively(offline_manual); + Common::FS::DeleteDirRecursively(offline_legal_information); + Common::FS::DeleteDirRecursively(offline_system_data); +} + GMainWindow::GMainWindow() : input_subsystem{std::make_shared()}, config{std::make_unique()}, vfs{std::make_shared()}, @@ -250,6 +272,9 @@ GMainWindow::GMainWindow() FileSys::ContentProviderUnionSlot::FrontendManual, provider.get()); Core::System::GetInstance().GetFileSystemController().CreateFactories(*vfs); + // Remove cached contents generated during the previous session + RemoveCachedContents(); + // Gen keys if necessary OnReinitializeKeys(ReinitializeKeyBehavior::NoWarning); @@ -341,6 +366,86 @@ void GMainWindow::SoftwareKeyboardInvokeCheckDialog(std::u16string error_message emit SoftwareKeyboardFinishedCheckDialog(); } +void GMainWindow::WebBrowserOpenLocalWebPage(std::string_view main_url, + std::string_view additional_args) { +#ifdef YUZU_USE_QT_WEB_ENGINE + + QtNXWebEngineView web_browser_view(this, Core::System::GetInstance()); + + web_browser_view.LoadLocalWebPage(main_url, additional_args); + + ui.action_Pause->setEnabled(false); + ui.action_Restart->setEnabled(false); + ui.action_Stop->setEnabled(false); + + if (render_window->IsLoadingComplete()) { + render_window->hide(); + } + + const auto& layout = render_window->GetFramebufferLayout(); + web_browser_view.resize(layout.screen.GetWidth(), layout.screen.GetHeight()); + web_browser_view.move(layout.screen.left, layout.screen.top + menuBar()->height()); + web_browser_view.setZoomFactor(static_cast(layout.screen.GetWidth()) / + static_cast(Layout::ScreenUndocked::Width)); + + web_browser_view.setFocus(); + web_browser_view.show(); + + bool exit_check = false; + + while (!web_browser_view.IsFinished()) { + QCoreApplication::processEvents(); + + if (!exit_check) { + web_browser_view.page()->runJavaScript( + QStringLiteral("end_applet;"), [&](const QVariant& variant) { + exit_check = false; + if (variant.toBool()) { + web_browser_view.SetFinished(true); + web_browser_view.SetExitReason(WebExitReason::EndButtonPressed); + } + }); + + exit_check = true; + } + + if (web_browser_view.GetCurrentURL().contains(QStringLiteral("localhost"))) { + if (!web_browser_view.IsFinished()) { + web_browser_view.SetFinished(true); + web_browser_view.SetExitReason(WebExitReason::CallbackURL); + } + + web_browser_view.SetLastURL(web_browser_view.GetCurrentURL().toStdString()); + } + + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + + const auto exit_reason = web_browser_view.GetExitReason(); + const auto last_url = web_browser_view.GetLastURL(); + + web_browser_view.hide(); + + render_window->setFocus(); + + if (render_window->IsLoadingComplete()) { + render_window->show(); + } + + ui.action_Pause->setEnabled(true); + ui.action_Restart->setEnabled(true); + ui.action_Stop->setEnabled(true); + + emit WebBrowserClosed(exit_reason, last_url); + +#else + + // Utilize the same fallback as the default web browser applet. + emit WebBrowserClosed(WebExitReason::WindowClosed, "http://localhost"); + +#endif +} + void GMainWindow::InitializeWidgets() { #ifdef YUZU_ENABLE_COMPATIBILITY_REPORTING ui.action_Report_Compatibility->setVisible(true); @@ -1948,6 +2053,7 @@ void GMainWindow::OnStartGame() { qRegisterMetaType("std::string"); qRegisterMetaType>("std::optional"); qRegisterMetaType("std::string_view"); + qRegisterMetaType("Service::AM::Applets::WebExitReason"); connect(emu_thread.get(), &EmuThread::ErrorThrown, this, &GMainWindow::OnCoreError); diff --git a/src/yuzu/main.h b/src/yuzu/main.h index f311f2b5b..22f64fc9c 100644 --- a/src/yuzu/main.h +++ b/src/yuzu/main.h @@ -55,6 +55,10 @@ namespace InputCommon { class InputSubsystem; } +namespace Service::AM::Applets { +enum class WebExitReason : u32; +} + enum class EmulatedDirectoryTarget { NAND, SDMC, @@ -126,6 +130,8 @@ signals: void SoftwareKeyboardFinishedText(std::optional text); void SoftwareKeyboardFinishedCheckDialog(); + void WebBrowserClosed(Service::AM::Applets::WebExitReason exit_reason, std::string last_url); + public slots: void OnLoadComplete(); void OnExecuteProgram(std::size_t program_index); @@ -135,6 +141,7 @@ public slots: void ProfileSelectorSelectProfile(); void SoftwareKeyboardGetText(const Core::Frontend::SoftwareKeyboardParameters& parameters); void SoftwareKeyboardInvokeCheckDialog(std::u16string error_message); + void WebBrowserOpenLocalWebPage(std::string_view main_url, std::string_view additional_args); void OnAppFocusStateChanged(Qt::ApplicationState state); private: -- cgit v1.2.3 From 8b95bf041da573459e953e27eee2dcf30208b02d Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Mon, 30 Nov 2020 10:15:00 -0500 Subject: main, applets/web: Re-add progress dialog for RomFS extraction --- src/core/frontend/applets/web_browser.cpp | 5 +- src/core/frontend/applets/web_browser.h | 11 ++--- src/core/hle/service/am/applets/web_browser.cpp | 66 +++++++++++++------------ src/core/hle/service/am/applets/web_browser.h | 10 ++++ src/yuzu/applets/web_browser.cpp | 21 +++++--- src/yuzu/applets/web_browser.h | 21 +++++--- src/yuzu/main.cpp | 58 ++++++++++++++++------ src/yuzu/main.h | 1 + 8 files changed, 125 insertions(+), 68 deletions(-) (limited to 'src/yuzu/applets/web_browser.h') diff --git a/src/core/frontend/applets/web_browser.cpp b/src/core/frontend/applets/web_browser.cpp index 0e1612e27..a5d8f82ac 100644 --- a/src/core/frontend/applets/web_browser.cpp +++ b/src/core/frontend/applets/web_browser.cpp @@ -12,11 +12,12 @@ WebBrowserApplet::~WebBrowserApplet() = default; DefaultWebBrowserApplet::~DefaultWebBrowserApplet() = default; void DefaultWebBrowserApplet::OpenLocalWebPage( - std::string_view local_url, std::function callback) const { + std::string_view local_url, std::function extract_romfs_callback, + std::function callback) const { LOG_WARNING(Service_AM, "(STUBBED) called, backend requested to open local web page at {}", local_url); - callback(WebExitReason::WindowClosed, "http://localhost/"); + callback(Service::AM::Applets::WebExitReason::WindowClosed, "http://localhost/"); } } // namespace Core::Frontend diff --git a/src/core/frontend/applets/web_browser.h b/src/core/frontend/applets/web_browser.h index 2ccefc68f..5b0629cfb 100644 --- a/src/core/frontend/applets/web_browser.h +++ b/src/core/frontend/applets/web_browser.h @@ -9,8 +9,6 @@ #include "core/hle/service/am/applets/web_types.h" -using namespace Service::AM::Applets; - namespace Core::Frontend { class WebBrowserApplet { @@ -18,16 +16,17 @@ public: virtual ~WebBrowserApplet(); virtual void OpenLocalWebPage( - std::string_view local_url, - std::function callback) const = 0; + std::string_view local_url, std::function extract_romfs_callback, + std::function callback) const = 0; }; class DefaultWebBrowserApplet final : public WebBrowserApplet { public: ~DefaultWebBrowserApplet() override; - void OpenLocalWebPage(std::string_view local_url, - std::function callback) const override; + void OpenLocalWebPage(std::string_view local_url, std::function extract_romfs_callback, + std::function + callback) const override; }; } // namespace Core::Frontend diff --git a/src/core/hle/service/am/applets/web_browser.cpp b/src/core/hle/service/am/applets/web_browser.cpp index 02ce9f387..9c8be156f 100644 --- a/src/core/hle/service/am/applets/web_browser.cpp +++ b/src/core/hle/service/am/applets/web_browser.cpp @@ -15,7 +15,6 @@ #include "core/file_sys/registered_cache.h" #include "core/file_sys/romfs.h" #include "core/file_sys/system_archive/system_archive.h" -#include "core/file_sys/vfs_types.h" #include "core/file_sys/vfs_vector.h" #include "core/frontend/applets/web_browser.h" #include "core/hle/kernel/process.h" @@ -311,6 +310,18 @@ void WebBrowser::Execute() { } } +void WebBrowser::ExtractOfflineRomFS() { + LOG_DEBUG(Service_AM, "Extracting RomFS to {}", offline_cache_dir); + + const auto extracted_romfs_dir = + FileSys::ExtractRomFS(offline_romfs, FileSys::RomFSExtractionType::SingleDiscard); + + const auto temp_dir = + system.GetFilesystem()->CreateDirectory(offline_cache_dir, FileSys::Mode::ReadWrite); + + FileSys::VfsRawCopyD(extracted_romfs_dir, temp_dir); +} + void WebBrowser::WebBrowserExit(WebExitReason exit_reason, std::string last_url) { if ((web_arg_header.shim_kind == ShimKind::Share && web_applet_version >= WebAppletVersion::Version196608) || @@ -360,12 +371,11 @@ void WebBrowser::InitializeOffline() { const auto document_kind = ParseRawValue(GetInputTLVData(WebArgInputTLVType::DocumentKind).value()); - u64 title_id{}; - FileSys::ContentRecordType nca_type{FileSys::ContentRecordType::HtmlDocument}; std::string additional_paths; switch (document_kind) { case DocumentKind::OfflineHtmlPage: + default: title_id = system.CurrentProcess()->GetTitleID(); nca_type = FileSys::ContentRecordType::HtmlDocument; additional_paths = "html-document"; @@ -395,31 +405,6 @@ void WebBrowser::InitializeOffline() { offline_document = Common::FS::SanitizePath( fmt::format("{}/{}/{}", offline_cache_dir, additional_paths, document_path), Common::FS::DirectorySeparator::PlatformDefault); - - const auto main_url = Common::FS::SanitizePath(GetMainURL(offline_document), - Common::FS::DirectorySeparator::PlatformDefault); - - if (Common::FS::Exists(main_url)) { - return; - } - - auto offline_romfs = GetOfflineRomFS(system, title_id, nca_type); - - if (offline_romfs == nullptr) { - LOG_ERROR(Service_AM, "RomFS with title_id={:016X} and nca_type={} cannot be extracted!", - title_id, nca_type); - return; - } - - LOG_DEBUG(Service_AM, "Extracting RomFS to {}", offline_cache_dir); - - const auto extracted_romfs_dir = - FileSys::ExtractRomFS(offline_romfs, FileSys::RomFSExtractionType::SingleDiscard); - - const auto temp_dir = - system.GetFilesystem()->CreateDirectory(offline_cache_dir, FileSys::Mode::ReadWrite); - - FileSys::VfsRawCopyD(extracted_romfs_dir, temp_dir); } void WebBrowser::InitializeShare() {} @@ -441,11 +426,28 @@ void WebBrowser::ExecuteLogin() { } void WebBrowser::ExecuteOffline() { + const auto main_url = Common::FS::SanitizePath(GetMainURL(offline_document), + Common::FS::DirectorySeparator::PlatformDefault); + + if (!Common::FS::Exists(main_url)) { + offline_romfs = GetOfflineRomFS(system, title_id, nca_type); + + if (offline_romfs == nullptr) { + LOG_ERROR(Service_AM, + "RomFS with title_id={:016X} and nca_type={} cannot be extracted!", title_id, + nca_type); + WebBrowserExit(WebExitReason::WindowClosed); + return; + } + } + LOG_INFO(Service_AM, "Opening offline document at {}", offline_document); - frontend.OpenLocalWebPage(offline_document, - [this](WebExitReason exit_reason, std::string last_url) { - WebBrowserExit(exit_reason, last_url); - }); + + frontend.OpenLocalWebPage( + offline_document, [this] { ExtractOfflineRomFS(); }, + [this](WebExitReason exit_reason, std::string last_url) { + WebBrowserExit(exit_reason, last_url); + }); } void WebBrowser::ExecuteShare() { diff --git a/src/core/hle/service/am/applets/web_browser.h b/src/core/hle/service/am/applets/web_browser.h index c36c717f1..936a49a86 100644 --- a/src/core/hle/service/am/applets/web_browser.h +++ b/src/core/hle/service/am/applets/web_browser.h @@ -8,6 +8,7 @@ #include "common/common_funcs.h" #include "common/common_types.h" +#include "core/file_sys/vfs_types.h" #include "core/hle/result.h" #include "core/hle/service/am/applets/applets.h" #include "core/hle/service/am/applets/web_types.h" @@ -16,6 +17,10 @@ namespace Core { class System; } +namespace FileSys { +enum class ContentRecordType : u8; +} + namespace Service::AM::Applets { class WebBrowser final : public Applet { @@ -31,6 +36,8 @@ public: void ExecuteInteractive() override; void Execute() override; + void ExtractOfflineRomFS(); + void WebBrowserExit(WebExitReason exit_reason, std::string last_url = ""); private: @@ -66,8 +73,11 @@ private: WebArgHeader web_arg_header; WebArgInputTLVMap web_arg_input_tlv_map; + u64 title_id; + FileSys::ContentRecordType nca_type; std::string offline_cache_dir; std::string offline_document; + FileSys::VirtualFile offline_romfs; Core::System& system; }; diff --git a/src/yuzu/applets/web_browser.cpp b/src/yuzu/applets/web_browser.cpp index 26b9df51a..52c99d1ba 100644 --- a/src/yuzu/applets/web_browser.cpp +++ b/src/yuzu/applets/web_browser.cpp @@ -120,7 +120,7 @@ QtNXWebEngineView::QtNXWebEngineView(QWidget* parent, Core::System& system) [this] { if (page()->url() == url_interceptor->GetRequestedURL()) { SetFinished(true); - SetExitReason(WebExitReason::WindowClosed); + SetExitReason(Service::AM::Applets::WebExitReason::WindowClosed); } }, Qt::QueuedConnection); @@ -135,7 +135,7 @@ void QtNXWebEngineView::LoadLocalWebPage(std::string_view main_url, std::string_view additional_args) { SetUserAgent(UserAgent::WebApplet); SetFinished(false); - SetExitReason(WebExitReason::EndButtonPressed); + SetExitReason(Service::AM::Applets::WebExitReason::EndButtonPressed); SetLastURL("http://localhost/"); StartInputThread(); @@ -176,11 +176,11 @@ void QtNXWebEngineView::SetFinished(bool finished_) { finished = finished_; } -WebExitReason QtNXWebEngineView::GetExitReason() const { +Service::AM::Applets::WebExitReason QtNXWebEngineView::GetExitReason() const { return exit_reason; } -void QtNXWebEngineView::SetExitReason(WebExitReason exit_reason_) { +void QtNXWebEngineView::SetExitReason(Service::AM::Applets::WebExitReason exit_reason_) { exit_reason = exit_reason_; } @@ -316,6 +316,8 @@ void QtNXWebEngineView::InputThread() { QtWebBrowser::QtWebBrowser(GMainWindow& main_window) { connect(this, &QtWebBrowser::MainWindowOpenLocalWebPage, &main_window, &GMainWindow::WebBrowserOpenLocalWebPage, Qt::QueuedConnection); + connect(&main_window, &GMainWindow::WebBrowserExtractOfflineRomFS, this, + &QtWebBrowser::MainWindowExtractOfflineRomFS, Qt::QueuedConnection); connect(&main_window, &GMainWindow::WebBrowserClosed, this, &QtWebBrowser::MainWindowWebBrowserClosed, Qt::QueuedConnection); } @@ -323,7 +325,9 @@ QtWebBrowser::QtWebBrowser(GMainWindow& main_window) { QtWebBrowser::~QtWebBrowser() = default; void QtWebBrowser::OpenLocalWebPage( - std::string_view local_url, std::function callback) const { + std::string_view local_url, std::function extract_romfs_callback, + std::function callback) const { + this->extract_romfs_callback = std::move(extract_romfs_callback); this->callback = std::move(callback); const auto index = local_url.find('?'); @@ -335,6 +339,11 @@ void QtWebBrowser::OpenLocalWebPage( } } -void QtWebBrowser::MainWindowWebBrowserClosed(WebExitReason exit_reason, std::string last_url) { +void QtWebBrowser::MainWindowExtractOfflineRomFS() { + extract_romfs_callback(); +} + +void QtWebBrowser::MainWindowWebBrowserClosed(Service::AM::Applets::WebExitReason exit_reason, + std::string last_url) { callback(exit_reason, last_url); } diff --git a/src/yuzu/applets/web_browser.h b/src/yuzu/applets/web_browser.h index 74f2b49d2..18b8640a7 100644 --- a/src/yuzu/applets/web_browser.h +++ b/src/yuzu/applets/web_browser.h @@ -69,8 +69,8 @@ public: [[nodiscard]] bool IsFinished() const; void SetFinished(bool finished_); - [[nodiscard]] WebExitReason GetExitReason() const; - void SetExitReason(WebExitReason exit_reason_); + [[nodiscard]] Service::AM::Applets::WebExitReason GetExitReason() const; + void SetExitReason(Service::AM::Applets::WebExitReason exit_reason_); [[nodiscard]] const std::string& GetLastURL() const; void SetLastURL(std::string last_url_); @@ -148,7 +148,8 @@ private: std::atomic finished{}; - WebExitReason exit_reason{WebExitReason::EndButtonPressed}; + Service::AM::Applets::WebExitReason exit_reason{ + Service::AM::Applets::WebExitReason::EndButtonPressed}; std::string last_url{"http://localhost/"}; }; @@ -162,15 +163,21 @@ public: explicit QtWebBrowser(GMainWindow& parent); ~QtWebBrowser() override; - void OpenLocalWebPage(std::string_view local_url, - std::function callback) const override; + void OpenLocalWebPage(std::string_view local_url, std::function extract_romfs_callback, + std::function + callback) const override; signals: void MainWindowOpenLocalWebPage(std::string_view main_url, std::string_view additional_args) const; private: - void MainWindowWebBrowserClosed(WebExitReason exit_reason, std::string last_url); + void MainWindowExtractOfflineRomFS(); - mutable std::function callback; + void MainWindowWebBrowserClosed(Service::AM::Applets::WebExitReason exit_reason, + std::string last_url); + + mutable std::function extract_romfs_callback; + + mutable std::function callback; }; diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index bab76db1e..f696fc494 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -372,24 +372,49 @@ void GMainWindow::WebBrowserOpenLocalWebPage(std::string_view main_url, QtNXWebEngineView web_browser_view(this, Core::System::GetInstance()); - web_browser_view.LoadLocalWebPage(main_url, additional_args); - ui.action_Pause->setEnabled(false); ui.action_Restart->setEnabled(false); ui.action_Stop->setEnabled(false); - if (render_window->IsLoadingComplete()) { - render_window->hide(); - } + { + QProgressDialog loading_progress(this); + loading_progress.setLabelText(tr("Loading Web Applet...")); + loading_progress.setRange(0, 3); + loading_progress.setValue(0); + + if (!Common::FS::Exists(std::string(main_url))) { + loading_progress.show(); + + auto future = QtConcurrent::run([this] { emit WebBrowserExtractOfflineRomFS(); }); + + while (!future.isFinished()) { + QCoreApplication::processEvents(); + } + } - const auto& layout = render_window->GetFramebufferLayout(); - web_browser_view.resize(layout.screen.GetWidth(), layout.screen.GetHeight()); - web_browser_view.move(layout.screen.left, layout.screen.top + menuBar()->height()); - web_browser_view.setZoomFactor(static_cast(layout.screen.GetWidth()) / - static_cast(Layout::ScreenUndocked::Width)); + loading_progress.setValue(1); - web_browser_view.setFocus(); - web_browser_view.show(); + web_browser_view.LoadLocalWebPage(main_url, additional_args); + + if (render_window->IsLoadingComplete()) { + render_window->hide(); + } + + const auto& layout = render_window->GetFramebufferLayout(); + web_browser_view.resize(layout.screen.GetWidth(), layout.screen.GetHeight()); + web_browser_view.move(layout.screen.left, layout.screen.top + menuBar()->height()); + web_browser_view.setZoomFactor(static_cast(layout.screen.GetWidth()) / + static_cast(Layout::ScreenUndocked::Width)); + + web_browser_view.setFocus(); + web_browser_view.show(); + + loading_progress.setValue(2); + + QCoreApplication::processEvents(); + + loading_progress.setValue(3); + } bool exit_check = false; @@ -402,7 +427,8 @@ void GMainWindow::WebBrowserOpenLocalWebPage(std::string_view main_url, exit_check = false; if (variant.toBool()) { web_browser_view.SetFinished(true); - web_browser_view.SetExitReason(WebExitReason::EndButtonPressed); + web_browser_view.SetExitReason( + Service::AM::Applets::WebExitReason::EndButtonPressed); } }); @@ -412,7 +438,7 @@ void GMainWindow::WebBrowserOpenLocalWebPage(std::string_view main_url, if (web_browser_view.GetCurrentURL().contains(QStringLiteral("localhost"))) { if (!web_browser_view.IsFinished()) { web_browser_view.SetFinished(true); - web_browser_view.SetExitReason(WebExitReason::CallbackURL); + web_browser_view.SetExitReason(Service::AM::Applets::WebExitReason::CallbackURL); } web_browser_view.SetLastURL(web_browser_view.GetCurrentURL().toStdString()); @@ -436,12 +462,14 @@ void GMainWindow::WebBrowserOpenLocalWebPage(std::string_view main_url, ui.action_Restart->setEnabled(true); ui.action_Stop->setEnabled(true); + QCoreApplication::processEvents(); + emit WebBrowserClosed(exit_reason, last_url); #else // Utilize the same fallback as the default web browser applet. - emit WebBrowserClosed(WebExitReason::WindowClosed, "http://localhost"); + emit WebBrowserClosed(Service::AM::Applets::WebExitReason::WindowClosed, "http://localhost"); #endif } diff --git a/src/yuzu/main.h b/src/yuzu/main.h index 22f64fc9c..ed02df3e2 100644 --- a/src/yuzu/main.h +++ b/src/yuzu/main.h @@ -130,6 +130,7 @@ signals: void SoftwareKeyboardFinishedText(std::optional text); void SoftwareKeyboardFinishedCheckDialog(); + void WebBrowserExtractOfflineRomFS(); void WebBrowserClosed(Service::AM::Applets::WebExitReason exit_reason, std::string last_url); public slots: -- cgit v1.2.3 From 51cddcb8b8d4d12715ba0517638b394244b500bb Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Mon, 30 Nov 2020 22:25:01 -0500 Subject: applets/web: Fix keyboard to emulated controller input --- src/yuzu/applets/web_browser.cpp | 19 +++++++++++++++++-- src/yuzu/applets/web_browser.h | 13 ++++++++++++- src/yuzu/main.cpp | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) (limited to 'src/yuzu/applets/web_browser.h') diff --git a/src/yuzu/applets/web_browser.cpp b/src/yuzu/applets/web_browser.cpp index 52c99d1ba..7e2dc6ee9 100644 --- a/src/yuzu/applets/web_browser.cpp +++ b/src/yuzu/applets/web_browser.cpp @@ -15,6 +15,8 @@ #include "common/file_util.h" #include "core/core.h" #include "core/frontend/input_interpreter.h" +#include "input_common/keyboard.h" +#include "input_common/main.h" #include "yuzu/applets/web_browser.h" #include "yuzu/applets/web_browser_scripts.h" #include "yuzu/main.h" @@ -45,8 +47,10 @@ constexpr int HIDButtonToKey(HIDButton button) { } // Anonymous namespace -QtNXWebEngineView::QtNXWebEngineView(QWidget* parent, Core::System& system) - : QWebEngineView(parent), url_interceptor(std::make_unique()), +QtNXWebEngineView::QtNXWebEngineView(QWidget* parent, Core::System& system, + InputCommon::InputSubsystem* input_subsystem_) + : QWebEngineView(parent), input_subsystem{input_subsystem_}, + url_interceptor(std::make_unique()), input_interpreter(std::make_unique(system)) { QWebEngineScript nx_font_css; QWebEngineScript load_nx_font; @@ -203,6 +207,14 @@ void QtNXWebEngineView::hide() { QWidget::hide(); } +void QtNXWebEngineView::keyPressEvent(QKeyEvent* event) { + input_subsystem->GetKeyboard()->PressKey(event->key()); +} + +void QtNXWebEngineView::keyReleaseEvent(QKeyEvent* event) { + input_subsystem->GetKeyboard()->ReleaseKey(event->key()); +} + template void QtNXWebEngineView::HandleWindowFooterButtonPressedOnce() { const auto f = [this](HIDButton button) { @@ -282,6 +294,7 @@ void QtNXWebEngineView::StartInputThread() { } void QtNXWebEngineView::StopInputThread() { + QWidget::releaseKeyboard(); input_thread_running = false; if (input_thread.joinable()) { input_thread.join(); @@ -292,6 +305,8 @@ void QtNXWebEngineView::InputThread() { // Wait for 1 second before allowing any inputs to be processed. std::this_thread::sleep_for(std::chrono::seconds(1)); + QWidget::grabKeyboard(); + while (input_thread_running) { input_interpreter->PollInput(); diff --git a/src/yuzu/applets/web_browser.h b/src/yuzu/applets/web_browser.h index 18b8640a7..cfddaa6f8 100644 --- a/src/yuzu/applets/web_browser.h +++ b/src/yuzu/applets/web_browser.h @@ -26,6 +26,10 @@ namespace Core { class System; } +namespace InputCommon { +class InputSubsystem; +} + #ifdef YUZU_USE_QT_WEB_ENGINE enum class UserAgent { @@ -41,7 +45,8 @@ class QtNXWebEngineView : public QWebEngineView { Q_OBJECT public: - explicit QtNXWebEngineView(QWidget* parent, Core::System& system); + explicit QtNXWebEngineView(QWidget* parent, Core::System& system, + InputCommon::InputSubsystem* input_subsystem_); ~QtNXWebEngineView() override; /** @@ -86,6 +91,10 @@ public: public slots: void hide(); +protected: + void keyPressEvent(QKeyEvent* event) override; + void keyReleaseEvent(QKeyEvent* event) override; + private: /** * Handles button presses to execute functions assigned in yuzu_key_callbacks. @@ -138,6 +147,8 @@ private: /// The thread where input is being polled and processed. void InputThread(); + InputCommon::InputSubsystem* input_subsystem; + std::unique_ptr url_interceptor; std::unique_ptr input_interpreter; diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index ccff08074..f835ee9cb 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -376,7 +376,7 @@ void GMainWindow::WebBrowserOpenLocalWebPage(std::string_view main_url, return; } - QtNXWebEngineView web_browser_view(this, Core::System::GetInstance()); + QtNXWebEngineView web_browser_view(this, Core::System::GetInstance(), input_subsystem.get()); ui.action_Pause->setEnabled(false); ui.action_Restart->setEnabled(false); -- cgit v1.2.3 From 82fa9f8d56bc285e7bb58fc81b495a55be9ea82c Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Tue, 8 Dec 2020 06:20:45 -0500 Subject: applets/web: Implement the online web browser applet --- src/core/frontend/applets/web_browser.cpp | 9 ++ src/core/frontend/applets/web_browser.h | 8 ++ src/core/hle/service/am/applets/web_browser.cpp | 12 +- src/core/hle/service/am/applets/web_browser.h | 2 + src/yuzu/applets/web_browser.cpp | 148 ++++++++++++++++-------- src/yuzu/applets/web_browser.h | 33 +++++- src/yuzu/main.cpp | 16 ++- src/yuzu/main.h | 3 +- 8 files changed, 167 insertions(+), 64 deletions(-) (limited to 'src/yuzu/applets/web_browser.h') diff --git a/src/core/frontend/applets/web_browser.cpp b/src/core/frontend/applets/web_browser.cpp index a5d8f82ac..50db6a654 100644 --- a/src/core/frontend/applets/web_browser.cpp +++ b/src/core/frontend/applets/web_browser.cpp @@ -20,4 +20,13 @@ void DefaultWebBrowserApplet::OpenLocalWebPage( callback(Service::AM::Applets::WebExitReason::WindowClosed, "http://localhost/"); } +void DefaultWebBrowserApplet::OpenExternalWebPage( + std::string_view external_url, + std::function callback) const { + LOG_WARNING(Service_AM, "(STUBBED) called, backend requested to open external web page at {}", + external_url); + + callback(Service::AM::Applets::WebExitReason::WindowClosed, "http://localhost/"); +} + } // namespace Core::Frontend diff --git a/src/core/frontend/applets/web_browser.h b/src/core/frontend/applets/web_browser.h index 5b0629cfb..1c5ef19a9 100644 --- a/src/core/frontend/applets/web_browser.h +++ b/src/core/frontend/applets/web_browser.h @@ -18,6 +18,10 @@ public: virtual void OpenLocalWebPage( std::string_view local_url, std::function extract_romfs_callback, std::function callback) const = 0; + + virtual void OpenExternalWebPage( + std::string_view external_url, + std::function callback) const = 0; }; class DefaultWebBrowserApplet final : public WebBrowserApplet { @@ -27,6 +31,10 @@ public: void OpenLocalWebPage(std::string_view local_url, std::function extract_romfs_callback, std::function callback) const override; + + void OpenExternalWebPage(std::string_view external_url, + std::function + callback) const override; }; } // namespace Core::Frontend diff --git a/src/core/hle/service/am/applets/web_browser.cpp b/src/core/hle/service/am/applets/web_browser.cpp index 9c8be156f..2ab420789 100644 --- a/src/core/hle/service/am/applets/web_browser.cpp +++ b/src/core/hle/service/am/applets/web_browser.cpp @@ -409,7 +409,9 @@ void WebBrowser::InitializeOffline() { void WebBrowser::InitializeShare() {} -void WebBrowser::InitializeWeb() {} +void WebBrowser::InitializeWeb() { + external_url = ParseStringValue(GetInputTLVData(WebArgInputTLVType::InitialURL).value()); +} void WebBrowser::InitializeWifi() {} @@ -456,8 +458,12 @@ void WebBrowser::ExecuteShare() { } void WebBrowser::ExecuteWeb() { - LOG_WARNING(Service_AM, "(STUBBED) called, Web Applet is not implemented"); - WebBrowserExit(WebExitReason::EndButtonPressed); + LOG_INFO(Service_AM, "Opening external URL at {}", external_url); + + frontend.OpenExternalWebPage(external_url, + [this](WebExitReason exit_reason, std::string last_url) { + WebBrowserExit(exit_reason, last_url); + }); } void WebBrowser::ExecuteWifi() { diff --git a/src/core/hle/service/am/applets/web_browser.h b/src/core/hle/service/am/applets/web_browser.h index 936a49a86..04c274754 100644 --- a/src/core/hle/service/am/applets/web_browser.h +++ b/src/core/hle/service/am/applets/web_browser.h @@ -79,6 +79,8 @@ private: std::string offline_document; FileSys::VirtualFile offline_romfs; + std::string external_url; + Core::System& system; }; diff --git a/src/yuzu/applets/web_browser.cpp b/src/yuzu/applets/web_browser.cpp index 7e2dc6ee9..e482ba029 100644 --- a/src/yuzu/applets/web_browser.cpp +++ b/src/yuzu/applets/web_browser.cpp @@ -51,59 +51,32 @@ QtNXWebEngineView::QtNXWebEngineView(QWidget* parent, Core::System& system, InputCommon::InputSubsystem* input_subsystem_) : QWebEngineView(parent), input_subsystem{input_subsystem_}, url_interceptor(std::make_unique()), - input_interpreter(std::make_unique(system)) { - QWebEngineScript nx_font_css; - QWebEngineScript load_nx_font; + input_interpreter(std::make_unique(system)), + default_profile{QWebEngineProfile::defaultProfile()}, + global_settings{QWebEngineSettings::globalSettings()} { QWebEngineScript gamepad; QWebEngineScript window_nx; - const QString fonts_dir = QString::fromStdString(Common::FS::SanitizePath( - fmt::format("{}/fonts", Common::FS::GetUserPath(Common::FS::UserPath::CacheDir)))); - - nx_font_css.setName(QStringLiteral("nx_font_css.js")); - load_nx_font.setName(QStringLiteral("load_nx_font.js")); gamepad.setName(QStringLiteral("gamepad_script.js")); window_nx.setName(QStringLiteral("window_nx_script.js")); - nx_font_css.setSourceCode( - QString::fromStdString(NX_FONT_CSS) - .arg(fonts_dir + QStringLiteral("/FontStandard.ttf")) - .arg(fonts_dir + QStringLiteral("/FontChineseSimplified.ttf")) - .arg(fonts_dir + QStringLiteral("/FontExtendedChineseSimplified.ttf")) - .arg(fonts_dir + QStringLiteral("/FontChineseTraditional.ttf")) - .arg(fonts_dir + QStringLiteral("/FontKorean.ttf")) - .arg(fonts_dir + QStringLiteral("/FontNintendoExtended.ttf")) - .arg(fonts_dir + QStringLiteral("/FontNintendoExtended2.ttf"))); - load_nx_font.setSourceCode(QString::fromStdString(LOAD_NX_FONT)); gamepad.setSourceCode(QString::fromStdString(GAMEPAD_SCRIPT)); window_nx.setSourceCode(QString::fromStdString(WINDOW_NX_SCRIPT)); - nx_font_css.setInjectionPoint(QWebEngineScript::DocumentReady); - load_nx_font.setInjectionPoint(QWebEngineScript::Deferred); gamepad.setInjectionPoint(QWebEngineScript::DocumentCreation); window_nx.setInjectionPoint(QWebEngineScript::DocumentCreation); - nx_font_css.setWorldId(QWebEngineScript::MainWorld); - load_nx_font.setWorldId(QWebEngineScript::MainWorld); gamepad.setWorldId(QWebEngineScript::MainWorld); window_nx.setWorldId(QWebEngineScript::MainWorld); - nx_font_css.setRunsOnSubFrames(true); - load_nx_font.setRunsOnSubFrames(true); gamepad.setRunsOnSubFrames(true); window_nx.setRunsOnSubFrames(true); - auto* default_profile = QWebEngineProfile::defaultProfile(); - - default_profile->scripts()->insert(nx_font_css); - default_profile->scripts()->insert(load_nx_font); default_profile->scripts()->insert(gamepad); default_profile->scripts()->insert(window_nx); default_profile->setRequestInterceptor(url_interceptor.get()); - auto* global_settings = QWebEngineSettings::globalSettings(); - global_settings->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true); global_settings->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true); global_settings->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, true); @@ -111,13 +84,7 @@ QtNXWebEngineView::QtNXWebEngineView(QWidget* parent, Core::System& system, global_settings->setAttribute(QWebEngineSettings::AllowWindowActivationFromJavaScript, true); global_settings->setAttribute(QWebEngineSettings::ShowScrollBars, false); - connect( - url_interceptor.get(), &UrlRequestInterceptor::FrameChanged, url_interceptor.get(), - [this] { - std::this_thread::sleep_for(std::chrono::milliseconds(50)); - page()->runJavaScript(QString::fromStdString(LOAD_NX_FONT)); - }, - Qt::QueuedConnection); + global_settings->setFontFamily(QWebEngineSettings::StandardFont, QStringLiteral("Roboto")); connect( page(), &QWebEnginePage::windowCloseRequested, page(), @@ -137,6 +104,9 @@ QtNXWebEngineView::~QtNXWebEngineView() { void QtNXWebEngineView::LoadLocalWebPage(std::string_view main_url, std::string_view additional_args) { + is_local = true; + + LoadExtractedFonts(); SetUserAgent(UserAgent::WebApplet); SetFinished(false); SetExitReason(Service::AM::Applets::WebExitReason::EndButtonPressed); @@ -147,6 +117,20 @@ void QtNXWebEngineView::LoadLocalWebPage(std::string_view main_url, QString::fromStdString(std::string(additional_args)))); } +void QtNXWebEngineView::LoadExternalWebPage(std::string_view main_url, + std::string_view additional_args) { + is_local = false; + + SetUserAgent(UserAgent::WebApplet); + SetFinished(false); + SetExitReason(Service::AM::Applets::WebExitReason::EndButtonPressed); + SetLastURL("http://localhost/"); + StartInputThread(); + + load(QUrl(QString::fromStdString(std::string(main_url)) + + QString::fromStdString(std::string(additional_args)))); +} + void QtNXWebEngineView::SetUserAgent(UserAgent user_agent) { const QString user_agent_str = [user_agent] { switch (user_agent) { @@ -208,11 +192,15 @@ void QtNXWebEngineView::hide() { } void QtNXWebEngineView::keyPressEvent(QKeyEvent* event) { - input_subsystem->GetKeyboard()->PressKey(event->key()); + if (is_local) { + input_subsystem->GetKeyboard()->PressKey(event->key()); + } } void QtNXWebEngineView::keyReleaseEvent(QKeyEvent* event) { - input_subsystem->GetKeyboard()->ReleaseKey(event->key()); + if (is_local) { + input_subsystem->GetKeyboard()->ReleaseKey(event->key()); + } } template @@ -294,7 +282,10 @@ void QtNXWebEngineView::StartInputThread() { } void QtNXWebEngineView::StopInputThread() { - QWidget::releaseKeyboard(); + if (is_local) { + QWidget::releaseKeyboard(); + } + input_thread_running = false; if (input_thread.joinable()) { input_thread.join(); @@ -305,7 +296,9 @@ void QtNXWebEngineView::InputThread() { // Wait for 1 second before allowing any inputs to be processed. std::this_thread::sleep_for(std::chrono::seconds(1)); - QWidget::grabKeyboard(); + if (is_local) { + QWidget::grabKeyboard(); + } while (input_thread_running) { input_interpreter->PollInput(); @@ -326,11 +319,53 @@ void QtNXWebEngineView::InputThread() { } } +void QtNXWebEngineView::LoadExtractedFonts() { + QWebEngineScript nx_font_css; + QWebEngineScript load_nx_font; + + const QString fonts_dir = QString::fromStdString(Common::FS::SanitizePath( + fmt::format("{}/fonts", Common::FS::GetUserPath(Common::FS::UserPath::CacheDir)))); + + nx_font_css.setName(QStringLiteral("nx_font_css.js")); + load_nx_font.setName(QStringLiteral("load_nx_font.js")); + + nx_font_css.setSourceCode( + QString::fromStdString(NX_FONT_CSS) + .arg(fonts_dir + QStringLiteral("/FontStandard.ttf")) + .arg(fonts_dir + QStringLiteral("/FontChineseSimplified.ttf")) + .arg(fonts_dir + QStringLiteral("/FontExtendedChineseSimplified.ttf")) + .arg(fonts_dir + QStringLiteral("/FontChineseTraditional.ttf")) + .arg(fonts_dir + QStringLiteral("/FontKorean.ttf")) + .arg(fonts_dir + QStringLiteral("/FontNintendoExtended.ttf")) + .arg(fonts_dir + QStringLiteral("/FontNintendoExtended2.ttf"))); + load_nx_font.setSourceCode(QString::fromStdString(LOAD_NX_FONT)); + + nx_font_css.setInjectionPoint(QWebEngineScript::DocumentReady); + load_nx_font.setInjectionPoint(QWebEngineScript::Deferred); + + nx_font_css.setWorldId(QWebEngineScript::MainWorld); + load_nx_font.setWorldId(QWebEngineScript::MainWorld); + + nx_font_css.setRunsOnSubFrames(true); + load_nx_font.setRunsOnSubFrames(true); + + default_profile->scripts()->insert(nx_font_css); + default_profile->scripts()->insert(load_nx_font); + + connect( + url_interceptor.get(), &UrlRequestInterceptor::FrameChanged, url_interceptor.get(), + [this] { + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + page()->runJavaScript(QString::fromStdString(LOAD_NX_FONT)); + }, + Qt::QueuedConnection); +} + #endif QtWebBrowser::QtWebBrowser(GMainWindow& main_window) { - connect(this, &QtWebBrowser::MainWindowOpenLocalWebPage, &main_window, - &GMainWindow::WebBrowserOpenLocalWebPage, Qt::QueuedConnection); + connect(this, &QtWebBrowser::MainWindowOpenWebPage, &main_window, + &GMainWindow::WebBrowserOpenWebPage, Qt::QueuedConnection); connect(&main_window, &GMainWindow::WebBrowserExtractOfflineRomFS, this, &QtWebBrowser::MainWindowExtractOfflineRomFS, Qt::QueuedConnection); connect(&main_window, &GMainWindow::WebBrowserClosed, this, @@ -340,17 +375,32 @@ QtWebBrowser::QtWebBrowser(GMainWindow& main_window) { QtWebBrowser::~QtWebBrowser() = default; void QtWebBrowser::OpenLocalWebPage( - std::string_view local_url, std::function extract_romfs_callback, - std::function callback) const { - this->extract_romfs_callback = std::move(extract_romfs_callback); - this->callback = std::move(callback); + std::string_view local_url, std::function extract_romfs_callback_, + std::function callback_) const { + extract_romfs_callback = std::move(extract_romfs_callback_); + callback = std::move(callback_); const auto index = local_url.find('?'); if (index == std::string::npos) { - emit MainWindowOpenLocalWebPage(local_url, ""); + emit MainWindowOpenWebPage(local_url, "", true); + } else { + emit MainWindowOpenWebPage(local_url.substr(0, index), local_url.substr(index), true); + } +} + +void QtWebBrowser::OpenExternalWebPage( + std::string_view external_url, + std::function callback_) const { + callback = std::move(callback_); + + const auto index = external_url.find('?'); + + if (index == std::string::npos) { + emit MainWindowOpenWebPage(external_url, "", false); } else { - emit MainWindowOpenLocalWebPage(local_url.substr(0, index), local_url.substr(index)); + emit MainWindowOpenWebPage(external_url.substr(0, index), external_url.substr(index), + false); } } diff --git a/src/yuzu/applets/web_browser.h b/src/yuzu/applets/web_browser.h index cfddaa6f8..47f960d69 100644 --- a/src/yuzu/applets/web_browser.h +++ b/src/yuzu/applets/web_browser.h @@ -18,8 +18,8 @@ enum class HIDButton : u8; -class InputInterpreter; class GMainWindow; +class InputInterpreter; class UrlRequestInterceptor; namespace Core { @@ -41,6 +41,9 @@ enum class UserAgent { WifiWebAuthApplet, }; +class QWebEngineProfile; +class QWebEngineSettings; + class QtNXWebEngineView : public QWebEngineView { Q_OBJECT @@ -57,6 +60,14 @@ public: */ void LoadLocalWebPage(std::string_view main_url, std::string_view additional_args); + /** + * Loads an external website. Cannot be used to load local urls. + * + * @param main_url The url to the website. + * @param additional_args Additional arguments appended to the main url. + */ + void LoadExternalWebPage(std::string_view main_url, std::string_view additional_args); + /** * Sets the background color of the web page. * @@ -147,6 +158,9 @@ private: /// The thread where input is being polled and processed. void InputThread(); + /// Loads the extracted fonts using JavaScript. + void LoadExtractedFonts(); + InputCommon::InputSubsystem* input_subsystem; std::unique_ptr url_interceptor; @@ -163,6 +177,11 @@ private: Service::AM::Applets::WebExitReason::EndButtonPressed}; std::string last_url{"http://localhost/"}; + + bool is_local{}; + + QWebEngineProfile* default_profile; + QWebEngineSettings* global_settings; }; #endif @@ -174,13 +193,17 @@ public: explicit QtWebBrowser(GMainWindow& parent); ~QtWebBrowser() override; - void OpenLocalWebPage(std::string_view local_url, std::function extract_romfs_callback, + void OpenLocalWebPage(std::string_view local_url, std::function extract_romfs_callback_, std::function - callback) const override; + callback_) const override; + + void OpenExternalWebPage(std::string_view external_url, + std::function + callback_) const override; signals: - void MainWindowOpenLocalWebPage(std::string_view main_url, - std::string_view additional_args) const; + void MainWindowOpenWebPage(std::string_view main_url, std::string_view additional_args, + bool is_local) const; private: void MainWindowExtractOfflineRomFS(); diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index f835ee9cb..620e80cdc 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -366,13 +366,13 @@ void GMainWindow::SoftwareKeyboardInvokeCheckDialog(std::u16string error_message emit SoftwareKeyboardFinishedCheckDialog(); } -void GMainWindow::WebBrowserOpenLocalWebPage(std::string_view main_url, - std::string_view additional_args) { +void GMainWindow::WebBrowserOpenWebPage(std::string_view main_url, std::string_view additional_args, + bool is_local) { #ifdef YUZU_USE_QT_WEB_ENGINE if (disable_web_applet) { emit WebBrowserClosed(Service::AM::Applets::WebExitReason::WindowClosed, - "http://localhost"); + "http://localhost/"); return; } @@ -388,7 +388,7 @@ void GMainWindow::WebBrowserOpenLocalWebPage(std::string_view main_url, loading_progress.setRange(0, 3); loading_progress.setValue(0); - if (!Common::FS::Exists(std::string(main_url))) { + if (is_local && !Common::FS::Exists(std::string(main_url))) { loading_progress.show(); auto future = QtConcurrent::run([this] { emit WebBrowserExtractOfflineRomFS(); }); @@ -400,7 +400,11 @@ void GMainWindow::WebBrowserOpenLocalWebPage(std::string_view main_url, loading_progress.setValue(1); - web_browser_view.LoadLocalWebPage(main_url, additional_args); + if (is_local) { + web_browser_view.LoadLocalWebPage(main_url, additional_args); + } else { + web_browser_view.LoadExternalWebPage(main_url, additional_args); + } if (render_window->IsLoadingComplete()) { render_window->hide(); @@ -493,7 +497,7 @@ void GMainWindow::WebBrowserOpenLocalWebPage(std::string_view main_url, #else // Utilize the same fallback as the default web browser applet. - emit WebBrowserClosed(Service::AM::Applets::WebExitReason::WindowClosed, "http://localhost"); + emit WebBrowserClosed(Service::AM::Applets::WebExitReason::WindowClosed, "http://localhost/"); #endif } diff --git a/src/yuzu/main.h b/src/yuzu/main.h index b140995bf..22f82b20e 100644 --- a/src/yuzu/main.h +++ b/src/yuzu/main.h @@ -142,7 +142,8 @@ public slots: void ProfileSelectorSelectProfile(); void SoftwareKeyboardGetText(const Core::Frontend::SoftwareKeyboardParameters& parameters); void SoftwareKeyboardInvokeCheckDialog(std::u16string error_message); - void WebBrowserOpenLocalWebPage(std::string_view main_url, std::string_view additional_args); + void WebBrowserOpenWebPage(std::string_view main_url, std::string_view additional_args, + bool is_local); void OnAppFocusStateChanged(Qt::ApplicationState state); private: -- cgit v1.2.3