apt: Implement additional applet state management. (#6303)

* apt: Implement additional library applet state management.

* kernel: Clear process handle table on exit.

* apt: Implement system applet commands.

* apt: Pop MediaType from command buffers with correct size.

* apt: Improve accuracy of parameters and HLE applet lifecycle.

* apt: General cleanup.

* file_sys: Make system save data open error code more correct.

Not sure if this is the exact right error code, but it's at least
more correct than before as Game Notes will now create its system
save data instead of throwing a fatal error.

* apt: Fix launching New 3DS Internet Browser.

* frd: Correct fix to GetMyScreenName response.
This commit is contained in:
Steveice10 2023-02-28 04:09:54 -08:00 committed by GitHub
parent 8b116aaa04
commit 3c15398f9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 966 additions and 705 deletions

View file

@ -6,6 +6,7 @@
#include <memory>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include "common/assert.h"
#include "common/common_types.h"
#include "core/core.h"
@ -42,24 +43,24 @@ static Core::TimingEventType* applet_update_event = nullptr;
/// The interval at which the Applet update callback will be called, 16.6ms
static const u64 applet_update_interval_us = 16666;
ResultCode Applet::Create(Service::APT::AppletId id,
std::weak_ptr<Service::APT::AppletManager> manager) {
ResultCode Applet::Create(Service::APT::AppletId id, Service::APT::AppletId parent, bool preload,
const std::shared_ptr<Service::APT::AppletManager>& manager) {
switch (id) {
case Service::APT::AppletId::SoftwareKeyboard1:
case Service::APT::AppletId::SoftwareKeyboard2:
applets[id] = std::make_shared<SoftwareKeyboard>(id, std::move(manager));
applets[id] = std::make_shared<SoftwareKeyboard>(id, parent, preload, manager);
break;
case Service::APT::AppletId::Ed1:
case Service::APT::AppletId::Ed2:
applets[id] = std::make_shared<MiiSelector>(id, std::move(manager));
applets[id] = std::make_shared<MiiSelector>(id, parent, preload, manager);
break;
case Service::APT::AppletId::Error:
case Service::APT::AppletId::Error2:
applets[id] = std::make_shared<ErrEula>(id, std::move(manager));
applets[id] = std::make_shared<ErrEula>(id, parent, preload, manager);
break;
case Service::APT::AppletId::Mint:
case Service::APT::AppletId::Mint2:
applets[id] = std::make_shared<Mint>(id, std::move(manager));
applets[id] = std::make_shared<Mint>(id, parent, preload, manager);
break;
default:
LOG_ERROR(Service_APT, "Could not create applet {}", id);
@ -68,6 +69,17 @@ ResultCode Applet::Create(Service::APT::AppletId id,
ErrorSummary::NotSupported, ErrorLevel::Permanent);
}
Service::APT::AppletAttributes attributes;
attributes.applet_pos.Assign(static_cast<u32>(Service::APT::AppletPos::AutoLibrary));
attributes.is_home_menu.Assign(false);
const auto lock_handle_data = manager->GetLockHandle(attributes);
manager->Initialize(id, lock_handle_data->corrected_attributes);
manager->Enable(lock_handle_data->corrected_attributes);
if (preload) {
manager->FinishPreloadingLibraryApplet(id);
}
return RESULT_SUCCESS;
}
@ -80,8 +92,8 @@ std::shared_ptr<Applet> Applet::Get(Service::APT::AppletId id) {
/// Handles updating the current Applet every time it's called.
static void AppletUpdateEvent(u64 applet_id, s64 cycles_late) {
Service::APT::AppletId id = static_cast<Service::APT::AppletId>(applet_id);
std::shared_ptr<Applet> applet = Applet::Get(id);
const auto id = static_cast<Service::APT::AppletId>(applet_id);
const auto applet = Applet::Get(id);
ASSERT_MSG(applet != nullptr, "Applet doesn't exist! applet_id={:08X}", id);
applet->Update();
@ -96,20 +108,28 @@ static void AppletUpdateEvent(u64 applet_id, s64 cycles_late) {
}
}
ResultCode Applet::Start(const Service::APT::AppletStartupParameter& parameter) {
ResultCode result = StartImpl(parameter);
if (result.IsError())
return result;
// Schedule the update event
Core::System::GetInstance().CoreTiming().ScheduleEvent(
usToCycles(applet_update_interval_us), applet_update_event, static_cast<u64>(id));
return result;
}
bool Applet::IsRunning() const {
return is_running;
}
ResultCode Applet::ReceiveParameter(const Service::APT::MessageParameter& parameter) {
switch (parameter.signal) {
case Service::APT::SignalType::Wakeup: {
ResultCode result = Start(parameter);
if (!result.IsError()) {
// Schedule the update event
Core::System::GetInstance().CoreTiming().ScheduleEvent(
usToCycles(applet_update_interval_us), applet_update_event, static_cast<u64>(id));
}
return result;
}
case Service::APT::SignalType::WakeupByCancel:
return Finalize();
default:
return ReceiveParameterImpl(parameter);
}
}
void Applet::SendParameter(const Service::APT::MessageParameter& parameter) {
if (auto locked = manager.lock()) {
locked->CancelAndSendParameter(parameter);
@ -118,12 +138,15 @@ void Applet::SendParameter(const Service::APT::MessageParameter& parameter) {
}
}
bool IsLibraryAppletRunning() {
// Check the applets map for instances of any applet
for (auto itr = applets.begin(); itr != applets.end(); ++itr)
if (itr->second != nullptr)
return true;
return false;
void Applet::CloseApplet(std::shared_ptr<Kernel::Object> object, const std::vector<u8>& buffer) {
if (auto locked = manager.lock()) {
locked->PrepareToCloseLibraryApplet(true, false, false);
locked->CloseLibraryApplet(std::move(object), buffer);
} else {
LOG_ERROR(Service_APT, "called after destructing applet manager");
}
is_running = false;
}
void Init() {

View file

@ -18,10 +18,12 @@ public:
* Creates an instance of the Applet subclass identified by the parameter.
* and stores it in a global map.
* @param id Id of the applet to create.
* @param parent Id of the applet's parent.
* @param preload Whether the applet is being preloaded.
* @returns ResultCode Whether the operation was successful or not.
*/
static ResultCode Create(Service::APT::AppletId id,
std::weak_ptr<Service::APT::AppletManager> manager);
static ResultCode Create(Service::APT::AppletId id, Service::APT::AppletId parent, bool preload,
const std::shared_ptr<Service::APT::AppletManager>& manager);
/**
* Retrieves the Applet instance identified by the specified id.
@ -35,19 +37,12 @@ public:
* @param parameter Parameter data to handle.
* @returns ResultCode Whether the operation was successful or not.
*/
virtual ResultCode ReceiveParameter(const Service::APT::MessageParameter& parameter) = 0;
/**
* Handles the Applet start event, triggered from the application.
* @param parameter Parameter data to handle.
* @returns ResultCode Whether the operation was successful or not.
*/
ResultCode Start(const Service::APT::AppletStartupParameter& parameter);
ResultCode ReceiveParameter(const Service::APT::MessageParameter& parameter);
/**
* Whether the applet is currently executing instead of the host application or not.
*/
bool IsRunning() const;
[[nodiscard]] bool IsRunning() const;
/**
* Handles an update tick for the Applet, lets it update the screen, send commands, etc.
@ -55,31 +50,45 @@ public:
virtual void Update() = 0;
protected:
Applet(Service::APT::AppletId id, std::weak_ptr<Service::APT::AppletManager> manager)
: id(id), manager(std::move(manager)) {}
Applet(Service::APT::AppletId id, Service::APT::AppletId parent, bool preload,
std::weak_ptr<Service::APT::AppletManager> manager)
: id(id), parent(parent), preload(preload), manager(std::move(manager)) {}
/**
* Handles a parameter from the application.
* @param parameter Parameter data to handle.
* @returns ResultCode Whether the operation was successful or not.
*/
virtual ResultCode ReceiveParameterImpl(const Service::APT::MessageParameter& parameter) = 0;
/**
* Handles the Applet start event, triggered from the application.
* @param parameter Parameter data to handle.
* @returns ResultCode Whether the operation was successful or not.
*/
virtual ResultCode StartImpl(const Service::APT::AppletStartupParameter& parameter) = 0;
virtual ResultCode Start(const Service::APT::MessageParameter& parameter) = 0;
/**
* Sends the LibAppletClosing signal to the application,
* along with the relevant data buffers.
*/
virtual ResultCode Finalize() = 0;
Service::APT::AppletId id; ///< Id of this Applet
Service::APT::AppletId parent; ///< Id of this Applet's parent
bool preload; ///< Whether the Applet is being preloaded.
std::shared_ptr<std::vector<u8>> heap_memory; ///< Heap memory for this Applet
/// Whether this applet is currently running instead of the host application or not.
bool is_running = false;
void SendParameter(const Service::APT::MessageParameter& parameter);
void CloseApplet(std::shared_ptr<Kernel::Object> object, const std::vector<u8>& buffer);
private:
std::weak_ptr<Service::APT::AppletManager> manager;
};
/// Returns whether a library applet is currently running
bool IsLibraryAppletRunning();
/// Initializes the HLE applets
void Init();

View file

@ -9,7 +9,7 @@
namespace HLE::Applets {
ResultCode ErrEula::ReceiveParameter(const Service::APT::MessageParameter& parameter) {
ResultCode ErrEula::ReceiveParameterImpl(const Service::APT::MessageParameter& parameter) {
if (parameter.signal != Service::APT::SignalType::Request) {
LOG_ERROR(Service_APT, "unsupported signal {}", parameter.signal);
UNIMPLEMENTED();
@ -33,34 +33,33 @@ ResultCode ErrEula::ReceiveParameter(const Service::APT::MessageParameter& param
"ErrEula Memory");
// Send the response message with the newly created SharedMemory
Service::APT::MessageParameter result;
result.signal = Service::APT::SignalType::Response;
result.buffer.clear();
result.destination_id = Service::APT::AppletId::Application;
result.sender_id = id;
result.object = framebuffer_memory;
SendParameter({
.sender_id = id,
.destination_id = parent,
.signal = Service::APT::SignalType::Response,
.object = framebuffer_memory,
});
SendParameter(result);
return RESULT_SUCCESS;
}
ResultCode ErrEula::StartImpl(const Service::APT::AppletStartupParameter& parameter) {
ResultCode ErrEula::Start(const Service::APT::MessageParameter& parameter) {
is_running = true;
startup_param = parameter.buffer;
// TODO(Subv): Set the expected fields in the response buffer before resending it to the
// application.
// TODO(Subv): Reverse the parameter format for the ErrEula applet
// Let the application know that we're closing
Service::APT::MessageParameter message;
message.buffer.resize(parameter.buffer.size());
std::fill(message.buffer.begin(), message.buffer.end(), 0);
message.signal = Service::APT::SignalType::WakeupByExit;
message.destination_id = Service::APT::AppletId::Application;
message.sender_id = id;
SendParameter(message);
// Let the application know that we're closing.
Finalize();
return RESULT_SUCCESS;
}
is_running = false;
ResultCode ErrEula::Finalize() {
std::vector<u8> buffer(startup_param.size());
std::fill(buffer.begin(), buffer.end(), 0);
CloseApplet(nullptr, buffer);
return RESULT_SUCCESS;
}

View file

@ -11,11 +11,13 @@ namespace HLE::Applets {
class ErrEula final : public Applet {
public:
explicit ErrEula(Service::APT::AppletId id, std::weak_ptr<Service::APT::AppletManager> manager)
: Applet(id, std::move(manager)) {}
explicit ErrEula(Service::APT::AppletId id, Service::APT::AppletId parent, bool preload,
std::weak_ptr<Service::APT::AppletManager> manager)
: Applet(id, parent, preload, std::move(manager)) {}
ResultCode ReceiveParameter(const Service::APT::MessageParameter& parameter) override;
ResultCode StartImpl(const Service::APT::AppletStartupParameter& parameter) override;
ResultCode ReceiveParameterImpl(const Service::APT::MessageParameter& parameter) override;
ResultCode Start(const Service::APT::MessageParameter& parameter) override;
ResultCode Finalize() override;
void Update() override;
private:
@ -23,6 +25,9 @@ private:
/// It holds the framebuffer info retrieved by the application with
/// GSPGPU::ImportDisplayCaptureInfo
std::shared_ptr<Kernel::SharedMemory> framebuffer_memory;
/// Parameter received by the applet on start.
std::vector<u8> startup_param;
};
} // namespace HLE::Applets

View file

@ -19,7 +19,7 @@
namespace HLE::Applets {
ResultCode MiiSelector::ReceiveParameter(const Service::APT::MessageParameter& parameter) {
ResultCode MiiSelector::ReceiveParameterImpl(const Service::APT::MessageParameter& parameter) {
if (parameter.signal != Service::APT::SignalType::Request) {
LOG_ERROR(Service_APT, "unsupported signal {}", parameter.signal);
UNIMPLEMENTED();
@ -42,18 +42,17 @@ ResultCode MiiSelector::ReceiveParameter(const Service::APT::MessageParameter& p
"MiiSelector Memory");
// Send the response message with the newly created SharedMemory
Service::APT::MessageParameter result;
result.signal = Service::APT::SignalType::Response;
result.buffer.clear();
result.destination_id = Service::APT::AppletId::Application;
result.sender_id = id;
result.object = framebuffer_memory;
SendParameter({
.sender_id = id,
.destination_id = parent,
.signal = Service::APT::SignalType::Response,
.object = framebuffer_memory,
});
SendParameter(result);
return RESULT_SUCCESS;
}
ResultCode MiiSelector::StartImpl(const Service::APT::AppletStartupParameter& parameter) {
ResultCode MiiSelector::Start(const Service::APT::MessageParameter& parameter) {
ASSERT_MSG(parameter.buffer.size() == sizeof(config),
"The size of the parameter (MiiConfig) is wrong");
@ -85,17 +84,11 @@ void MiiSelector::Update() {
Finalize();
}
void MiiSelector::Finalize() {
// Let the application know that we're closing
Service::APT::MessageParameter message;
message.buffer.resize(sizeof(MiiResult));
std::memcpy(message.buffer.data(), &result, message.buffer.size());
message.signal = Service::APT::SignalType::WakeupByExit;
message.destination_id = Service::APT::AppletId::Application;
message.sender_id = id;
SendParameter(message);
is_running = false;
ResultCode MiiSelector::Finalize() {
std::vector<u8> buffer(sizeof(MiiResult));
std::memcpy(buffer.data(), &result, buffer.size());
CloseApplet(nullptr, buffer);
return RESULT_SUCCESS;
}
MiiResult MiiSelector::GetStandardMiiResult() {

View file

@ -115,19 +115,15 @@ ASSERT_REG_POSITION(guest_mii_name, 0x6C);
class MiiSelector final : public Applet {
public:
MiiSelector(Service::APT::AppletId id, std::weak_ptr<Service::APT::AppletManager> manager)
: Applet(id, std::move(manager)) {}
MiiSelector(Service::APT::AppletId id, Service::APT::AppletId parent, bool preload,
std::weak_ptr<Service::APT::AppletManager> manager)
: Applet(id, parent, preload, std::move(manager)) {}
ResultCode ReceiveParameter(const Service::APT::MessageParameter& parameter) override;
ResultCode StartImpl(const Service::APT::AppletStartupParameter& parameter) override;
ResultCode ReceiveParameterImpl(const Service::APT::MessageParameter& parameter) override;
ResultCode Start(const Service::APT::MessageParameter& parameter) override;
ResultCode Finalize() override;
void Update() override;
/**
* Sends the LibAppletClosing signal to the application,
* along with the relevant data buffers.
*/
void Finalize();
static MiiResult GetStandardMiiResult();
private:

View file

@ -9,7 +9,7 @@
namespace HLE::Applets {
ResultCode Mint::ReceiveParameter(const Service::APT::MessageParameter& parameter) {
ResultCode Mint::ReceiveParameterImpl(const Service::APT::MessageParameter& parameter) {
if (parameter.signal != Service::APT::SignalType::Request) {
LOG_ERROR(Service_APT, "unsupported signal {}", parameter.signal);
UNIMPLEMENTED();
@ -33,34 +33,33 @@ ResultCode Mint::ReceiveParameter(const Service::APT::MessageParameter& paramete
"Mint Memory");
// Send the response message with the newly created SharedMemory
Service::APT::MessageParameter result;
result.signal = Service::APT::SignalType::Response;
result.buffer.clear();
result.destination_id = Service::APT::AppletId::Application;
result.sender_id = id;
result.object = framebuffer_memory;
SendParameter({
.sender_id = id,
.destination_id = parent,
.signal = Service::APT::SignalType::Response,
.object = framebuffer_memory,
});
SendParameter(result);
return RESULT_SUCCESS;
}
ResultCode Mint::StartImpl(const Service::APT::AppletStartupParameter& parameter) {
ResultCode Mint::Start(const Service::APT::MessageParameter& parameter) {
is_running = true;
startup_param = parameter.buffer;
// TODO(Subv): Set the expected fields in the response buffer before resending it to the
// application.
// TODO(Subv): Reverse the parameter format for the Mint applet
// Let the application know that we're closing
Service::APT::MessageParameter message;
message.buffer.resize(parameter.buffer.size());
std::fill(message.buffer.begin(), message.buffer.end(), 0);
message.signal = Service::APT::SignalType::WakeupByExit;
message.destination_id = Service::APT::AppletId::Application;
message.sender_id = id;
SendParameter(message);
Finalize();
return RESULT_SUCCESS;
}
is_running = false;
ResultCode Mint::Finalize() {
std::vector<u8> buffer(startup_param.size());
std::fill(buffer.begin(), buffer.end(), 0);
CloseApplet(nullptr, buffer);
return RESULT_SUCCESS;
}

View file

@ -11,11 +11,13 @@ namespace HLE::Applets {
class Mint final : public Applet {
public:
explicit Mint(Service::APT::AppletId id, std::weak_ptr<Service::APT::AppletManager> manager)
: Applet(id, std::move(manager)) {}
explicit Mint(Service::APT::AppletId id, Service::APT::AppletId parent, bool preload,
std::weak_ptr<Service::APT::AppletManager> manager)
: Applet(id, parent, preload, std::move(manager)) {}
ResultCode ReceiveParameter(const Service::APT::MessageParameter& parameter) override;
ResultCode StartImpl(const Service::APT::AppletStartupParameter& parameter) override;
ResultCode ReceiveParameterImpl(const Service::APT::MessageParameter& parameter) override;
ResultCode Start(const Service::APT::MessageParameter& parameter) override;
ResultCode Finalize() override;
void Update() override;
private:
@ -23,6 +25,9 @@ private:
/// It holds the framebuffer info retrieved by the application with
/// GSPGPU::ImportDisplayCaptureInfo
std::shared_ptr<Kernel::SharedMemory> framebuffer_memory;
/// Parameter received by the applet on start.
std::vector<u8> startup_param;
};
} // namespace HLE::Applets

View file

@ -21,7 +21,7 @@
namespace HLE::Applets {
ResultCode SoftwareKeyboard::ReceiveParameter(Service::APT::MessageParameter const& parameter) {
ResultCode SoftwareKeyboard::ReceiveParameterImpl(Service::APT::MessageParameter const& parameter) {
switch (parameter.signal) {
case Service::APT::SignalType::Request: {
// The LibAppJustStarted message contains a buffer with the size of the framebuffer shared
@ -39,14 +39,13 @@ ResultCode SoftwareKeyboard::ReceiveParameter(Service::APT::MessageParameter con
"SoftwareKeyboard Memory");
// Send the response message with the newly created SharedMemory
Service::APT::MessageParameter result;
result.signal = Service::APT::SignalType::Response;
result.buffer.clear();
result.destination_id = Service::APT::AppletId::Application;
result.sender_id = id;
result.object = framebuffer_memory;
SendParameter({
.sender_id = id,
.destination_id = parent,
.signal = Service::APT::SignalType::Response,
.object = framebuffer_memory,
});
SendParameter(result);
return RESULT_SUCCESS;
}
@ -92,7 +91,7 @@ ResultCode SoftwareKeyboard::ReceiveParameter(Service::APT::MessageParameter con
}
}
ResultCode SoftwareKeyboard::StartImpl(Service::APT::AppletStartupParameter const& parameter) {
ResultCode SoftwareKeyboard::Start(Service::APT::MessageParameter const& parameter) {
ASSERT_MSG(parameter.buffer.size() == sizeof(config),
"The size of the parameter (SoftwareKeyboardConfig) is wrong");
@ -151,14 +150,16 @@ void SoftwareKeyboard::Update() {
config.text_offset = 0;
if (config.filter_flags & HLE::Applets::SoftwareKeyboardFilter::Callback) {
std::vector<u8> buffer(sizeof(SoftwareKeyboardConfig));
std::memcpy(buffer.data(), &config, buffer.size());
// Send the message to invoke callback
Service::APT::MessageParameter message;
message.buffer.resize(sizeof(SoftwareKeyboardConfig));
std::memcpy(message.buffer.data(), &config, message.buffer.size());
message.signal = Service::APT::SignalType::Message;
message.destination_id = Service::APT::AppletId::Application;
message.sender_id = id;
SendParameter(message);
SendParameter({
.sender_id = id,
.destination_id = parent,
.signal = Service::APT::SignalType::Message,
.buffer = buffer,
});
} else {
Finalize();
}
@ -168,18 +169,12 @@ void SoftwareKeyboard::DrawScreenKeyboard() {
// TODO(Subv): Draw the HLE keyboard, for now just do nothing
}
void SoftwareKeyboard::Finalize() {
// Let the application know that we're closing
Service::APT::MessageParameter message;
message.buffer.resize(sizeof(SoftwareKeyboardConfig));
std::memcpy(message.buffer.data(), &config, message.buffer.size());
message.signal = Service::APT::SignalType::WakeupByExit;
message.destination_id = Service::APT::AppletId::Application;
message.sender_id = id;
SendParameter(message);
is_running = false;
ResultCode SoftwareKeyboard::Finalize() {
std::vector<u8> buffer(sizeof(SoftwareKeyboardConfig));
std::memcpy(buffer.data(), &config, buffer.size());
CloseApplet(nullptr, buffer);
text_memory = nullptr;
return RESULT_SUCCESS;
}
Frontend::KeyboardConfig SoftwareKeyboard::ToFrontendConfig(

View file

@ -175,11 +175,13 @@ static_assert(sizeof(SoftwareKeyboardConfig) == 0x400, "Software Keyboard Config
class SoftwareKeyboard final : public Applet {
public:
SoftwareKeyboard(Service::APT::AppletId id, std::weak_ptr<Service::APT::AppletManager> manager)
: Applet(id, std::move(manager)) {}
SoftwareKeyboard(Service::APT::AppletId id, Service::APT::AppletId parent, bool preload,
std::weak_ptr<Service::APT::AppletManager> manager)
: Applet(id, parent, preload, std::move(manager)) {}
ResultCode ReceiveParameter(const Service::APT::MessageParameter& parameter) override;
ResultCode StartImpl(const Service::APT::AppletStartupParameter& parameter) override;
ResultCode ReceiveParameterImpl(const Service::APT::MessageParameter& parameter) override;
ResultCode Start(const Service::APT::MessageParameter& parameter) override;
ResultCode Finalize() override;
void Update() override;
/**
@ -187,12 +189,6 @@ public:
*/
void DrawScreenKeyboard();
/**
* Sends the LibAppletClosing signal to the application,
* along with the relevant data buffers.
*/
void Finalize();
private:
Frontend::KeyboardConfig ToFrontendConfig(const SoftwareKeyboardConfig& config) const;