Merge pull request #5042 from Morph1984/project-aether

Project Aether: Reimplementation of the Web Browser Applet
This commit is contained in:
bunnei
2020-12-21 23:47:10 -08:00
committed by GitHub
27 changed files with 1826 additions and 899 deletions

View File

@@ -141,6 +141,8 @@ add_executable(yuzu
util/limitable_input_dialog.h
util/sequence_dialog/sequence_dialog.cpp
util/sequence_dialog/sequence_dialog.h
util/url_request_interceptor.cpp
util/url_request_interceptor.h
util/util.cpp
util/util.h
compatdb.cpp

View File

@@ -1,115 +1,414 @@
// Copyright 2018 yuzu Emulator Project
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <mutex>
#ifdef YUZU_USE_QT_WEB_ENGINE
#include <QKeyEvent>
#include "core/hle/lock.h"
#include <QWebEngineProfile>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include <QWebEngineSettings>
#include <QWebEngineUrlScheme>
#endif
#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"
#include "yuzu/util/url_request_interceptor.h"
#ifdef YUZU_USE_QT_WEB_ENGINE
constexpr char NX_SHIM_INJECT_SCRIPT[] = R"(
window.nx = {};
window.nx.playReport = {};
window.nx.playReport.setCounterSetIdentifier = function () {
console.log("nx.playReport.setCounterSetIdentifier called - unimplemented");
};
namespace {
window.nx.playReport.incrementCounter = function () {
console.log("nx.playReport.incrementCounter called - unimplemented");
};
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;
}
}
window.nx.footer = {};
window.nx.footer.unsetAssign = function () {
console.log("nx.footer.unsetAssign called - unimplemented");
};
} // Anonymous namespace
var yuzu_key_callbacks = [];
window.nx.footer.setAssign = function(key, discard1, func, discard2) {
switch (key) {
case 'A':
yuzu_key_callbacks[0] = func;
break;
case 'B':
yuzu_key_callbacks[1] = func;
break;
case 'X':
yuzu_key_callbacks[2] = func;
break;
case 'Y':
yuzu_key_callbacks[3] = func;
break;
case 'L':
yuzu_key_callbacks[6] = func;
break;
case 'R':
yuzu_key_callbacks[7] = func;
break;
QtNXWebEngineView::QtNXWebEngineView(QWidget* parent, Core::System& system,
InputCommon::InputSubsystem* input_subsystem_)
: QWebEngineView(parent), input_subsystem{input_subsystem_},
url_interceptor(std::make_unique<UrlRequestInterceptor>()),
input_interpreter(std::make_unique<InputInterpreter>(system)),
default_profile{QWebEngineProfile::defaultProfile()},
global_settings{QWebEngineSettings::globalSettings()} {
QWebEngineScript gamepad;
QWebEngineScript window_nx;
gamepad.setName(QStringLiteral("gamepad_script.js"));
window_nx.setName(QStringLiteral("window_nx_script.js"));
gamepad.setSourceCode(QString::fromStdString(GAMEPAD_SCRIPT));
window_nx.setSourceCode(QString::fromStdString(WINDOW_NX_SCRIPT));
gamepad.setInjectionPoint(QWebEngineScript::DocumentCreation);
window_nx.setInjectionPoint(QWebEngineScript::DocumentCreation);
gamepad.setWorldId(QWebEngineScript::MainWorld);
window_nx.setWorldId(QWebEngineScript::MainWorld);
gamepad.setRunsOnSubFrames(true);
window_nx.setRunsOnSubFrames(true);
default_profile->scripts()->insert(gamepad);
default_profile->scripts()->insert(window_nx);
default_profile->setRequestInterceptor(url_interceptor.get());
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);
global_settings->setFontFamily(QWebEngineSettings::StandardFont, QStringLiteral("Roboto"));
connect(
page(), &QWebEnginePage::windowCloseRequested, page(),
[this] {
if (page()->url() == url_interceptor->GetRequestedURL()) {
SetFinished(true);
SetExitReason(Service::AM::Applets::WebExitReason::WindowClosed);
}
},
Qt::QueuedConnection);
}
QtNXWebEngineView::~QtNXWebEngineView() {
SetFinished(true);
StopInputThread();
}
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);
SetLastURL("http://localhost/");
StartInputThread();
load(QUrl(QUrl::fromLocalFile(QString::fromStdString(std::string(main_url))).toString() +
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) {
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_;
}
Service::AM::Applets::WebExitReason QtNXWebEngineView::GetExitReason() const {
return exit_reason;
}
void QtNXWebEngineView::SetExitReason(Service::AM::Applets::WebExitReason exit_reason_) {
exit_reason = exit_reason_;
}
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();
}
void QtNXWebEngineView::keyPressEvent(QKeyEvent* event) {
if (is_local) {
input_subsystem->GetKeyboard()->PressKey(event->key());
}
}
void QtNXWebEngineView::keyReleaseEvent(QKeyEvent* event) {
if (is_local) {
input_subsystem->GetKeyboard()->ReleaseKey(event->key());
}
}
template <HIDButton... T>
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<u8>(button)),
[&](const QVariant& variant) {
if (variant.toBool()) {
switch (button) {
case HIDButton::A:
SendMultipleKeyPressEvents<Qt::Key_A, Qt::Key_Space, Qt::Key_Return>();
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<u8>(button)));
}
};
var applet_done = false;
window.nx.endApplet = function() {
applet_done = true;
(f(T), ...);
}
template <HIDButton... T>
void QtNXWebEngineView::HandleWindowKeyButtonPressedOnce() {
const auto f = [this](HIDButton button) {
if (input_interpreter->IsButtonPressedOnce(button)) {
SendKeyPressEvent(HIDButtonToKey(button));
}
};
window.onkeypress = function(e) { if (e.keyCode === 13) { applet_done = true; } };
)";
QString GetNXShimInjectionScript() {
return QString::fromStdString(NX_SHIM_INJECT_SCRIPT);
(f(T), ...);
}
NXInputWebEngineView::NXInputWebEngineView(QWidget* parent) : QWebEngineView(parent) {}
template <HIDButton... T>
void QtNXWebEngineView::HandleWindowKeyButtonHold() {
const auto f = [this](HIDButton button) {
if (input_interpreter->IsButtonHeld(button)) {
SendKeyPressEvent(HIDButtonToKey(button));
}
};
void NXInputWebEngineView::keyPressEvent(QKeyEvent* event) {
parent()->event(event);
(f(T), ...);
}
void NXInputWebEngineView::keyReleaseEvent(QKeyEvent* event) {
parent()->event(event);
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() {
if (is_local) {
QWidget::releaseKeyboard();
}
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));
if (is_local) {
QWidget::grabKeyboard();
}
while (input_thread_running) {
input_interpreter->PollInput();
HandleWindowFooterButtonPressedOnce<HIDButton::A, HIDButton::B, HIDButton::X, HIDButton::Y,
HIDButton::L, HIDButton::R>();
HandleWindowKeyButtonPressedOnce<HIDButton::DLeft, HIDButton::DUp, HIDButton::DRight,
HIDButton::DDown, HIDButton::LStickLeft,
HIDButton::LStickUp, HIDButton::LStickRight,
HIDButton::LStickDown>();
HandleWindowKeyButtonHold<HIDButton::DLeft, HIDButton::DUp, HIDButton::DRight,
HIDButton::DDown, HIDButton::LStickLeft, HIDButton::LStickUp,
HIDButton::LStickRight, HIDButton::LStickDown>();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
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::MainWindowOpenPage, &main_window, &GMainWindow::WebBrowserOpenPage,
Qt::QueuedConnection);
connect(&main_window, &GMainWindow::WebBrowserUnpackRomFS, this,
&QtWebBrowser::MainWindowUnpackRomFS, Qt::QueuedConnection);
connect(&main_window, &GMainWindow::WebBrowserFinishedBrowsing, this,
&QtWebBrowser::MainWindowFinishedBrowsing, 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,
&QtWebBrowser::MainWindowWebBrowserClosed, Qt::QueuedConnection);
}
QtWebBrowser::~QtWebBrowser() = default;
void QtWebBrowser::OpenPageLocal(std::string_view url, std::function<void()> unpack_romfs_callback_,
std::function<void()> finished_callback_) {
unpack_romfs_callback = std::move(unpack_romfs_callback_);
finished_callback = std::move(finished_callback_);
void QtWebBrowser::OpenLocalWebPage(
std::string_view local_url, std::function<void()> extract_romfs_callback_,
std::function<void(Service::AM::Applets::WebExitReason, std::string)> callback_) const {
extract_romfs_callback = std::move(extract_romfs_callback_);
callback = std::move(callback_);
const auto index = local_url.find('?');
const auto index = url.find('?');
if (index == std::string::npos) {
emit MainWindowOpenPage(url, "");
emit MainWindowOpenWebPage(local_url, "", true);
} else {
const auto front = url.substr(0, index);
const auto back = url.substr(index);
emit MainWindowOpenPage(front, back);
emit MainWindowOpenWebPage(local_url.substr(0, index), local_url.substr(index), true);
}
}
void QtWebBrowser::MainWindowUnpackRomFS() {
// Acquire the HLE mutex
std::lock_guard lock{HLE::g_hle_lock};
unpack_romfs_callback();
void QtWebBrowser::OpenExternalWebPage(
std::string_view external_url,
std::function<void(Service::AM::Applets::WebExitReason, std::string)> callback_) const {
callback = std::move(callback_);
const auto index = external_url.find('?');
if (index == std::string::npos) {
emit MainWindowOpenWebPage(external_url, "", false);
} else {
emit MainWindowOpenWebPage(external_url.substr(0, index), external_url.substr(index),
false);
}
}
void QtWebBrowser::MainWindowFinishedBrowsing() {
// Acquire the HLE mutex
std::lock_guard lock{HLE::g_hle_lock};
finished_callback();
void QtWebBrowser::MainWindowExtractOfflineRomFS() {
extract_romfs_callback();
}
void QtWebBrowser::MainWindowWebBrowserClosed(Service::AM::Applets::WebExitReason exit_reason,
std::string last_url) {
callback(exit_reason, last_url);
}

View File

@@ -1,10 +1,13 @@
// 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 <functional>
#include <atomic>
#include <memory>
#include <thread>
#include <QObject>
#ifdef YUZU_USE_QT_WEB_ENGINE
@@ -13,19 +16,172 @@
#include "core/frontend/applets/web_browser.h"
enum class HIDButton : u8;
class GMainWindow;
class InputInterpreter;
class UrlRequestInterceptor;
namespace Core {
class System;
}
namespace InputCommon {
class InputSubsystem;
}
#ifdef YUZU_USE_QT_WEB_ENGINE
QString GetNXShimInjectionScript();
enum class UserAgent {
WebApplet,
ShopN,
LoginApplet,
ShareApplet,
LobbyApplet,
WifiWebAuthApplet,
};
class QWebEngineProfile;
class QWebEngineSettings;
class QtNXWebEngineView : public QWebEngineView {
Q_OBJECT
class NXInputWebEngineView : public QWebEngineView {
public:
explicit NXInputWebEngineView(QWidget* parent = nullptr);
explicit QtNXWebEngineView(QWidget* parent, Core::System& system,
InputCommon::InputSubsystem* input_subsystem_);
~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);
/**
* 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.
*
* @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]] 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_);
/**
* 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();
protected:
void keyPressEvent(QKeyEvent* event) override;
void keyReleaseEvent(QKeyEvent* event) override;
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 <HIDButton... T>
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 <HIDButton... T>
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 <HIDButton... T>
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 <int... T>
void SendMultipleKeyPressEvents() {
(SendKeyPressEvent(T), ...);
}
void StartInputThread();
void StopInputThread();
/// 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<UrlRequestInterceptor> url_interceptor;
std::unique_ptr<InputInterpreter> input_interpreter;
std::thread input_thread;
std::atomic<bool> input_thread_running{};
std::atomic<bool> finished{};
Service::AM::Applets::WebExitReason exit_reason{
Service::AM::Applets::WebExitReason::EndButtonPressed};
std::string last_url{"http://localhost/"};
bool is_local{};
QWebEngineProfile* default_profile;
QWebEngineSettings* global_settings;
};
#endif
@@ -34,19 +190,28 @@ class QtWebBrowser final : public QObject, public Core::Frontend::WebBrowserAppl
Q_OBJECT
public:
explicit QtWebBrowser(GMainWindow& main_window);
explicit QtWebBrowser(GMainWindow& parent);
~QtWebBrowser() override;
void OpenPageLocal(std::string_view url, std::function<void()> unpack_romfs_callback_,
std::function<void()> finished_callback_) override;
void OpenLocalWebPage(std::string_view local_url, std::function<void()> extract_romfs_callback_,
std::function<void(Service::AM::Applets::WebExitReason, std::string)>
callback_) const override;
void OpenExternalWebPage(std::string_view external_url,
std::function<void(Service::AM::Applets::WebExitReason, std::string)>
callback_) const override;
signals:
void MainWindowOpenPage(std::string_view filename, std::string_view additional_args) const;
void MainWindowOpenWebPage(std::string_view main_url, std::string_view additional_args,
bool is_local) const;
private:
void MainWindowUnpackRomFS();
void MainWindowFinishedBrowsing();
void MainWindowExtractOfflineRomFS();
std::function<void()> unpack_romfs_callback;
std::function<void()> finished_callback;
void MainWindowWebBrowserClosed(Service::AM::Applets::WebExitReason exit_reason,
std::string last_url);
mutable std::function<void()> extract_romfs_callback;
mutable std::function<void(Service::AM::Applets::WebExitReason, std::string)> callback;
};

View File

@@ -0,0 +1,193 @@
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
constexpr char NX_FONT_CSS[] = R"(
(function() {
css = document.createElement('style');
css.type = 'text/css';
css.id = 'nx_font';
css.innerText = `
/* FontStandard */
@font-face {
font-family: 'FontStandard';
src: url('%1') format('truetype');
}
/* FontChineseSimplified */
@font-face {
font-family: 'FontChineseSimplified';
src: url('%2') format('truetype');
}
/* FontExtendedChineseSimplified */
@font-face {
font-family: 'FontExtendedChineseSimplified';
src: url('%3') format('truetype');
}
/* FontChineseTraditional */
@font-face {
font-family: 'FontChineseTraditional';
src: url('%4') format('truetype');
}
/* FontKorean */
@font-face {
font-family: 'FontKorean';
src: url('%5') format('truetype');
}
/* FontNintendoExtended */
@font-face {
font-family: 'NintendoExt003';
src: url('%6') format('truetype');
}
/* FontNintendoExtended2 */
@font-face {
font-family: 'NintendoExt003';
src: url('%7') format('truetype');
}
`;
document.head.appendChild(css);
})();
)";
constexpr char LOAD_NX_FONT[] = R"(
(function() {
var elements = document.querySelectorAll("*");
for (var i = 0; i < elements.length; i++) {
var style = window.getComputedStyle(elements[i], null);
if (style.fontFamily.includes("Arial") || style.fontFamily.includes("Calibri") ||
style.fontFamily.includes("Century") || style.fontFamily.includes("Times New Roman")) {
elements[i].style.fontFamily = "FontStandard, FontChineseSimplified, FontExtendedChineseSimplified, FontChineseTraditional, FontKorean, NintendoExt003";
} else {
elements[i].style.fontFamily = style.fontFamily + ", FontStandard, FontChineseSimplified, FontExtendedChineseSimplified, FontChineseTraditional, FontKorean, NintendoExt003";
}
}
})();
)";
constexpr char GAMEPAD_SCRIPT[] = R"(
window.addEventListener("gamepadconnected", function(e) {
console.log("Gamepad connected at index %d: %s. %d buttons, %d axes.",
e.gamepad.index, e.gamepad.id, e.gamepad.buttons.length, e.gamepad.axes.length);
});
window.addEventListener("gamepaddisconnected", function(e) {
console.log("Gamepad disconnected from index %d: %s", e.gamepad.index, e.gamepad.id);
});
)";
constexpr char WINDOW_NX_SCRIPT[] = R"(
var end_applet = false;
var yuzu_key_callbacks = [];
(function() {
class WindowNX {
constructor() {
yuzu_key_callbacks[1] = function() { window.history.back(); };
yuzu_key_callbacks[2] = function() { window.nx.endApplet(); };
}
addEventListener(type, listener, options) {
console.log("nx.addEventListener called, type=%s", type);
window.addEventListener(type, listener, options);
}
endApplet() {
console.log("nx.endApplet called");
end_applet = true;
}
playSystemSe(system_se) {
console.log("nx.playSystemSe is not implemented, system_se=%s", system_se);
}
sendMessage(message) {
console.log("nx.sendMessage is not implemented, message=%s", message);
}
setCursorScrollSpeed(scroll_speed) {
console.log("nx.setCursorScrollSpeed is not implemented, scroll_speed=%d", scroll_speed);
}
}
class WindowNXFooter {
setAssign(key, label, func, option) {
console.log("nx.footer.setAssign called, key=%s", key);
switch (key) {
case "A":
yuzu_key_callbacks[0] = func;
break;
case "B":
yuzu_key_callbacks[1] = func;
break;
case "X":
yuzu_key_callbacks[2] = func;
break;
case "Y":
yuzu_key_callbacks[3] = func;
break;
case "L":
yuzu_key_callbacks[6] = func;
break;
case "R":
yuzu_key_callbacks[7] = func;
break;
}
}
setFixed(kind) {
console.log("nx.footer.setFixed is not implemented, kind=%s", kind);
}
unsetAssign(key) {
console.log("nx.footer.unsetAssign called, key=%s", key);
switch (key) {
case "A":
yuzu_key_callbacks[0] = function() {};
break;
case "B":
yuzu_key_callbacks[1] = function() {};
break;
case "X":
yuzu_key_callbacks[2] = function() {};
break;
case "Y":
yuzu_key_callbacks[3] = function() {};
break;
case "L":
yuzu_key_callbacks[6] = function() {};
break;
case "R":
yuzu_key_callbacks[7] = function() {};
break;
}
}
}
class WindowNXPlayReport {
incrementCounter(counter_id) {
console.log("nx.playReport.incrementCounter is not implemented, counter_id=%d", counter_id);
}
setCounterSetIdentifier(counter_id) {
console.log("nx.playReport.setCounterSetIdentifier is not implemented, counter_id=%d", counter_id);
}
}
window.nx = new WindowNX();
window.nx.footer = new WindowNXFooter();
window.nx.playReport = new WindowNXPlayReport();
})();
)";

View File

@@ -569,6 +569,10 @@ void GRenderWindow::CaptureScreenshot(u32 res_scale, const QString& screenshot_p
layout);
}
bool GRenderWindow::IsLoadingComplete() const {
return first_frame;
}
void GRenderWindow::OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) {
setMinimumSize(minimal_size.first, minimal_size.second);
}

View File

@@ -162,6 +162,8 @@ public:
/// Destroy the previous run's child_widget which should also destroy the child_window
void ReleaseRenderTarget();
bool IsLoadingComplete() const;
void CaptureScreenshot(u32 res_scale, const QString& screenshot_path);
std::pair<u32, u32> ScaleTouch(const QPointF& pos) const;

View File

@@ -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.
@@ -125,14 +123,6 @@ static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::Virtual
#include "yuzu/discord_impl.h"
#endif
#ifdef YUZU_USE_QT_WEB_ENGINE
#include <QWebEngineProfile>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include <QWebEngineSettings>
#include <QWebEngineView>
#endif
#ifdef QT_STATICPLUGIN
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
#endif
@@ -190,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<InputCommon::InputSubsystem>()},
config{std::make_unique<Config>()}, vfs{std::make_shared<FileSys::RealVfsFilesystem>()},
@@ -258,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);
@@ -349,151 +366,142 @@ void GMainWindow::SoftwareKeyboardInvokeCheckDialog(std::u16string error_message
emit SoftwareKeyboardFinishedCheckDialog();
}
void GMainWindow::WebBrowserOpenWebPage(std::string_view main_url, std::string_view additional_args,
bool is_local) {
#ifdef YUZU_USE_QT_WEB_ENGINE
void GMainWindow::WebBrowserOpenPage(std::string_view filename, std::string_view additional_args) {
NXInputWebEngineView web_browser_view(this);
if (disable_web_applet) {
emit WebBrowserClosed(Service::AM::Applets::WebExitReason::WindowClosed,
"http://localhost/");
return;
}
QtNXWebEngineView web_browser_view(this, Core::System::GetInstance(), input_subsystem.get());
ui.action_Pause->setEnabled(false);
ui.action_Restart->setEnabled(false);
ui.action_Stop->setEnabled(false);
// Scope to contain the QProgressDialog for initialization
{
QProgressDialog progress(this);
progress.setMinimumDuration(200);
progress.setLabelText(tr("Loading Web Applet..."));
progress.setRange(0, 4);
progress.setValue(0);
progress.show();
QProgressDialog loading_progress(this);
loading_progress.setLabelText(tr("Loading Web Applet..."));
loading_progress.setRange(0, 3);
loading_progress.setValue(0);
auto future = QtConcurrent::run([this] { emit WebBrowserUnpackRomFS(); });
if (is_local && !Common::FS::Exists(std::string(main_url))) {
loading_progress.show();
while (!future.isFinished())
QApplication::processEvents();
auto future = QtConcurrent::run([this] { emit WebBrowserExtractOfflineRomFS(); });
progress.setValue(1);
while (!future.isFinished()) {
QCoreApplication::processEvents();
}
}
// Load the special shim script to handle input and exit.
QWebEngineScript nx_shim;
nx_shim.setSourceCode(GetNXShimInjectionScript());
nx_shim.setWorldId(QWebEngineScript::MainWorld);
nx_shim.setName(QStringLiteral("nx_inject.js"));
nx_shim.setInjectionPoint(QWebEngineScript::DocumentCreation);
nx_shim.setRunsOnSubFrames(true);
web_browser_view.page()->profile()->scripts()->insert(nx_shim);
loading_progress.setValue(1);
web_browser_view.load(
QUrl(QUrl::fromLocalFile(QString::fromStdString(std::string(filename))).toString() +
QString::fromStdString(std::string(additional_args))));
if (is_local) {
web_browser_view.LoadLocalWebPage(main_url, additional_args);
} else {
web_browser_view.LoadExternalWebPage(main_url, additional_args);
}
progress.setValue(2);
render_window->hide();
web_browser_view.setFocus();
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<qreal>(layout.screen.GetWidth()) /
Layout::ScreenUndocked::Width);
web_browser_view.settings()->setAttribute(
QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
static_cast<qreal>(Layout::ScreenUndocked::Width));
web_browser_view.setFocus();
web_browser_view.show();
progress.setValue(3);
loading_progress.setValue(2);
QApplication::processEvents();
QCoreApplication::processEvents();
progress.setValue(4);
loading_progress.setValue(3);
}
bool finished = false;
QAction* exit_action = new QAction(tr("Exit Web Applet"), this);
connect(exit_action, &QAction::triggered, this, [&finished] { finished = true; });
bool exit_check = false;
// TODO (Morph): Remove this
QAction* exit_action = new QAction(tr("Disable Web Applet"), this);
connect(exit_action, &QAction::triggered, this, [this, &web_browser_view] {
const auto result = QMessageBox::warning(
this, tr("Disable Web Applet"),
tr("Disabling the web applet will cause it to not be shown again for the rest of the "
"emulated session. This can lead to undefined behavior and should only be used with "
"Super Mario 3D All-Stars. Are you sure you want to disable the web applet?"),
QMessageBox::Yes | QMessageBox::No);
if (result == QMessageBox::Yes) {
disable_web_applet = true;
web_browser_view.SetFinished(true);
}
});
ui.menubar->addAction(exit_action);
auto& npad =
Core::System::GetInstance()
.ServiceManager()
.GetService<Service::HID::Hid>("hid")
->GetAppletResource()
->GetController<Service::HID::Controller_NPad>(Service::HID::HidController::NPad);
while (!web_browser_view.IsFinished()) {
QCoreApplication::processEvents();
const auto fire_js_keypress = [&web_browser_view](u32 key_code) {
web_browser_view.page()->runJavaScript(
QStringLiteral("document.dispatchEvent(new KeyboardEvent('keydown', {'key': %1}));")
.arg(key_code));
};
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(
Service::AM::Applets::WebExitReason::EndButtonPressed);
}
});
QMessageBox::information(
this, tr("Exit"),
tr("To exit the web application, use the game provided controls to select exit, select the "
"'Exit Web Applet' option in the menu bar, or press the 'Enter' key."));
bool running_exit_check = false;
while (!finished) {
QApplication::processEvents();
if (!running_exit_check) {
web_browser_view.page()->runJavaScript(QStringLiteral("applet_done;"),
[&](const QVariant& res) {
running_exit_check = false;
if (res.toBool())
finished = true;
});
running_exit_check = true;
exit_check = true;
}
const auto input = npad.GetAndResetPressState();
for (std::size_t i = 0; i < Settings::NativeButton::NumButtons; ++i) {
if ((input & (1 << i)) != 0) {
LOG_DEBUG(Frontend, "firing input for button id={:02X}", i);
web_browser_view.page()->runJavaScript(
QStringLiteral("yuzu_key_callbacks[%1]();").arg(i));
if (web_browser_view.GetCurrentURL().contains(QStringLiteral("localhost"))) {
if (!web_browser_view.IsFinished()) {
web_browser_view.SetFinished(true);
web_browser_view.SetExitReason(Service::AM::Applets::WebExitReason::CallbackURL);
}
web_browser_view.SetLastURL(web_browser_view.GetCurrentURL().toStdString());
}
if (input & 0x00888000) // RStick Down | LStick Down | DPad Down
fire_js_keypress(40); // Down Arrow Key
else if (input & 0x00444000) // RStick Right | LStick Right | DPad Right
fire_js_keypress(39); // Right Arrow Key
else if (input & 0x00222000) // RStick Up | LStick Up | DPad Up
fire_js_keypress(38); // Up Arrow Key
else if (input & 0x00111000) // RStick Left | LStick Left | DPad Left
fire_js_keypress(37); // Left Arrow Key
else if (input & 0x00000001) // A Button
fire_js_keypress(13); // Enter Key
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->show();
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);
ui.menubar->removeAction(exit_action);
// Needed to update render window focus/show and remove menubar action
QApplication::processEvents();
emit WebBrowserFinishedBrowsing();
}
QCoreApplication::processEvents();
emit WebBrowserClosed(exit_reason, last_url);
#else
void GMainWindow::WebBrowserOpenPage(std::string_view filename, std::string_view additional_args) {
#ifndef __linux__
QMessageBox::warning(
this, tr("Web Applet"),
tr("This version of yuzu was built without QtWebEngine support, meaning that yuzu cannot "
"properly display the game manual or web page requested."),
QMessageBox::Ok, QMessageBox::Ok);
// Utilize the same fallback as the default web browser applet.
emit WebBrowserClosed(Service::AM::Applets::WebExitReason::WindowClosed, "http://localhost/");
#endif
LOG_INFO(Frontend,
"(STUBBED) called - Missing QtWebEngine dependency needed to open website page at "
"'{}' with arguments '{}'!",
filename, additional_args);
emit WebBrowserFinishedBrowsing();
}
#endif
void GMainWindow::InitializeWidgets() {
#ifdef YUZU_ENABLE_COMPATIBILITY_REPORTING
ui.action_Report_Compatibility->setVisible(true);
@@ -993,7 +1001,6 @@ bool GMainWindow::LoadROM(const QString& filename, std::size_t program_index) {
system.SetAppletFrontendSet({
std::make_unique<QtControllerSelector>(*this), // Controller Selector
nullptr, // E-Commerce
std::make_unique<QtErrorDisplay>(*this), // Error Display
nullptr, // Parental Controls
nullptr, // Photo Viewer
@@ -2102,6 +2109,7 @@ void GMainWindow::OnStartGame() {
qRegisterMetaType<std::string>("std::string");
qRegisterMetaType<std::optional<std::u16string>>("std::optional<std::u16string>");
qRegisterMetaType<std::string_view>("std::string_view");
qRegisterMetaType<Service::AM::Applets::WebExitReason>("Service::AM::Applets::WebExitReason");
connect(emu_thread.get(), &EmuThread::ErrorThrown, this, &GMainWindow::OnCoreError);

View File

@@ -55,6 +55,10 @@ namespace InputCommon {
class InputSubsystem;
}
namespace Service::AM::Applets {
enum class WebExitReason : u32;
}
enum class EmulatedDirectoryTarget {
NAND,
SDMC,
@@ -126,8 +130,8 @@ signals:
void SoftwareKeyboardFinishedText(std::optional<std::u16string> text);
void SoftwareKeyboardFinishedCheckDialog();
void WebBrowserUnpackRomFS();
void WebBrowserFinishedBrowsing();
void WebBrowserExtractOfflineRomFS();
void WebBrowserClosed(Service::AM::Applets::WebExitReason exit_reason, std::string last_url);
public slots:
void OnLoadComplete();
@@ -138,7 +142,8 @@ public slots:
void ProfileSelectorSelectProfile();
void SoftwareKeyboardGetText(const Core::Frontend::SoftwareKeyboardParameters& parameters);
void SoftwareKeyboardInvokeCheckDialog(std::u16string error_message);
void WebBrowserOpenPage(std::string_view filename, std::string_view arguments);
void WebBrowserOpenWebPage(std::string_view main_url, std::string_view additional_args,
bool is_local);
void OnAppFocusStateChanged(Qt::ApplicationState state);
private:
@@ -321,6 +326,9 @@ private:
// Last game booted, used for multi-process apps
QString last_filename_booted;
// Disables the web applet for the rest of the emulated session
bool disable_web_applet{};
protected:
void dropEvent(QDropEvent* event) override;
void dragEnterEvent(QDragEnterEvent* event) override;

View File

@@ -0,0 +1,32 @@
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#ifdef YUZU_USE_QT_WEB_ENGINE
#include "yuzu/util/url_request_interceptor.h"
UrlRequestInterceptor::UrlRequestInterceptor(QObject* p) : QWebEngineUrlRequestInterceptor(p) {}
UrlRequestInterceptor::~UrlRequestInterceptor() = default;
void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo& info) {
const auto resource_type = info.resourceType();
switch (resource_type) {
case QWebEngineUrlRequestInfo::ResourceTypeMainFrame:
requested_url = info.requestUrl();
emit FrameChanged();
break;
case QWebEngineUrlRequestInfo::ResourceTypeSubFrame:
case QWebEngineUrlRequestInfo::ResourceTypeXhr:
emit FrameChanged();
break;
}
}
QUrl UrlRequestInterceptor::GetRequestedURL() const {
return requested_url;
}
#endif

View File

@@ -0,0 +1,30 @@
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#ifdef YUZU_USE_QT_WEB_ENGINE
#include <QObject>
#include <QWebEngineUrlRequestInterceptor>
class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor {
Q_OBJECT
public:
explicit UrlRequestInterceptor(QObject* p = nullptr);
~UrlRequestInterceptor() override;
void interceptRequest(QWebEngineUrlRequestInfo& info) override;
QUrl GetRequestedURL() const;
signals:
void FrameChanged();
private:
QUrl requested_url;
};
#endif