2015-05-26 04:30:20 +00:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-07-12 21:10:37 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <memory>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_map>
|
2023-02-28 12:09:54 +00:00
|
|
|
#include <utility>
|
2015-05-26 04:30:20 +00:00
|
|
|
#include "common/assert.h"
|
2015-07-12 21:10:37 +00:00
|
|
|
#include "common/common_types.h"
|
2018-10-27 19:53:20 +00:00
|
|
|
#include "core/core.h"
|
2015-05-26 16:00:26 +00:00
|
|
|
#include "core/core_timing.h"
|
2016-09-21 06:52:38 +00:00
|
|
|
#include "core/hle/applets/applet.h"
|
2016-03-30 12:52:59 +00:00
|
|
|
#include "core/hle/applets/erreula.h"
|
2015-12-04 21:05:23 +00:00
|
|
|
#include "core/hle/applets/mii_selector.h"
|
2017-01-31 09:16:58 +00:00
|
|
|
#include "core/hle/applets/mint.h"
|
2015-05-26 04:30:20 +00:00
|
|
|
#include "core/hle/applets/swkbd.h"
|
2015-07-12 21:10:37 +00:00
|
|
|
#include "core/hle/result.h"
|
2015-05-26 04:30:20 +00:00
|
|
|
|
2019-02-19 01:34:18 +00:00
|
|
|
namespace HLE::Applets {
|
2015-05-26 04:30:20 +00:00
|
|
|
|
|
|
|
static std::unordered_map<Service::APT::AppletId, std::shared_ptr<Applet>> applets;
|
2017-11-25 13:56:57 +00:00
|
|
|
/// The CoreTiming event identifier for the Applet update callback.
|
2018-10-27 19:53:20 +00:00
|
|
|
static Core::TimingEventType* applet_update_event = nullptr;
|
2015-05-27 20:21:06 +00:00
|
|
|
/// The interval at which the Applet update callback will be called, 16.6ms
|
|
|
|
static const u64 applet_update_interval_us = 16666;
|
2015-05-26 04:30:20 +00:00
|
|
|
|
2023-02-28 12:09:54 +00:00
|
|
|
ResultCode Applet::Create(Service::APT::AppletId id, Service::APT::AppletId parent, bool preload,
|
|
|
|
const std::shared_ptr<Service::APT::AppletManager>& manager) {
|
2015-05-26 04:30:20 +00:00
|
|
|
switch (id) {
|
|
|
|
case Service::APT::AppletId::SoftwareKeyboard1:
|
|
|
|
case Service::APT::AppletId::SoftwareKeyboard2:
|
2023-02-28 12:09:54 +00:00
|
|
|
applets[id] = std::make_shared<SoftwareKeyboard>(id, parent, preload, manager);
|
2015-05-26 04:30:20 +00:00
|
|
|
break;
|
2015-12-04 21:05:23 +00:00
|
|
|
case Service::APT::AppletId::Ed1:
|
|
|
|
case Service::APT::AppletId::Ed2:
|
2023-02-28 12:09:54 +00:00
|
|
|
applets[id] = std::make_shared<MiiSelector>(id, parent, preload, manager);
|
2015-12-04 21:05:23 +00:00
|
|
|
break;
|
2016-03-30 12:52:59 +00:00
|
|
|
case Service::APT::AppletId::Error:
|
|
|
|
case Service::APT::AppletId::Error2:
|
2023-02-28 12:09:54 +00:00
|
|
|
applets[id] = std::make_shared<ErrEula>(id, parent, preload, manager);
|
2016-03-30 12:52:59 +00:00
|
|
|
break;
|
2017-01-31 09:16:58 +00:00
|
|
|
case Service::APT::AppletId::Mint:
|
|
|
|
case Service::APT::AppletId::Mint2:
|
2023-02-28 12:09:54 +00:00
|
|
|
applets[id] = std::make_shared<Mint>(id, parent, preload, manager);
|
2017-01-31 09:16:58 +00:00
|
|
|
break;
|
2015-05-26 04:30:20 +00:00
|
|
|
default:
|
2020-12-29 05:39:21 +00:00
|
|
|
LOG_ERROR(Service_APT, "Could not create applet {}", id);
|
2015-05-26 04:30:20 +00:00
|
|
|
// TODO(Subv): Find the right error code
|
2016-09-18 00:38:01 +00:00
|
|
|
return ResultCode(ErrorDescription::NotFound, ErrorModule::Applet,
|
|
|
|
ErrorSummary::NotSupported, ErrorLevel::Permanent);
|
2015-05-26 04:30:20 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 12:09:54 +00:00
|
|
|
Service::APT::AppletAttributes attributes;
|
2023-03-09 23:44:26 +00:00
|
|
|
attributes.applet_pos.Assign(Service::APT::AppletPos::AutoLibrary);
|
2023-02-28 12:09:54 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-03-25 21:36:14 +00:00
|
|
|
// Schedule the update event
|
|
|
|
Core::System::GetInstance().CoreTiming().ScheduleEvent(
|
|
|
|
usToCycles(applet_update_interval_us), applet_update_event, static_cast<u64>(id));
|
2015-05-26 04:30:20 +00:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Applet> Applet::Get(Service::APT::AppletId id) {
|
|
|
|
auto itr = applets.find(id);
|
|
|
|
if (itr != applets.end())
|
|
|
|
return itr->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-26 16:00:26 +00:00
|
|
|
/// Handles updating the current Applet every time it's called.
|
2018-07-23 21:08:14 +00:00
|
|
|
static void AppletUpdateEvent(u64 applet_id, s64 cycles_late) {
|
2023-02-28 12:09:54 +00:00
|
|
|
const auto id = static_cast<Service::APT::AppletId>(applet_id);
|
|
|
|
const auto applet = Applet::Get(id);
|
2020-12-29 05:39:21 +00:00
|
|
|
ASSERT_MSG(applet != nullptr, "Applet doesn't exist! applet_id={:08X}", id);
|
2015-05-26 16:00:26 +00:00
|
|
|
|
2023-03-25 21:36:14 +00:00
|
|
|
if (applet->IsActive()) {
|
|
|
|
applet->Update();
|
|
|
|
}
|
2015-05-27 20:21:06 +00:00
|
|
|
|
|
|
|
// If the applet is still running after the last update, reschedule the event
|
|
|
|
if (applet->IsRunning()) {
|
2018-10-27 19:53:20 +00:00
|
|
|
Core::System::GetInstance().CoreTiming().ScheduleEvent(
|
|
|
|
usToCycles(applet_update_interval_us) - cycles_late, applet_update_event, applet_id);
|
2015-05-27 20:21:06 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise the applet has terminated, in which case we should clean it up
|
|
|
|
applets[id] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 22:11:39 +00:00
|
|
|
bool Applet::IsRunning() const {
|
|
|
|
return is_running;
|
|
|
|
}
|
|
|
|
|
2023-03-25 21:36:14 +00:00
|
|
|
bool Applet::IsActive() const {
|
|
|
|
return is_active;
|
|
|
|
}
|
|
|
|
|
2023-02-28 12:09:54 +00:00
|
|
|
ResultCode Applet::ReceiveParameter(const Service::APT::MessageParameter& parameter) {
|
|
|
|
switch (parameter.signal) {
|
|
|
|
case Service::APT::SignalType::Wakeup: {
|
|
|
|
ResultCode result = Start(parameter);
|
|
|
|
if (!result.IsError()) {
|
2023-03-25 21:36:14 +00:00
|
|
|
is_active = true;
|
2023-02-28 12:09:54 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
case Service::APT::SignalType::WakeupByCancel:
|
|
|
|
return Finalize();
|
|
|
|
default:
|
|
|
|
return ReceiveParameterImpl(parameter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 13:39:54 +00:00
|
|
|
void Applet::SendParameter(const Service::APT::MessageParameter& parameter) {
|
|
|
|
if (auto locked = manager.lock()) {
|
|
|
|
locked->CancelAndSendParameter(parameter);
|
|
|
|
} else {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_ERROR(Service_APT, "called after destructing applet manager");
|
2018-01-25 13:39:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 12:09:54 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2023-03-25 21:36:14 +00:00
|
|
|
is_active = false;
|
2023-02-28 12:09:54 +00:00
|
|
|
is_running = false;
|
2015-07-23 23:52:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-26 16:00:26 +00:00
|
|
|
void Init() {
|
2015-05-27 20:21:06 +00:00
|
|
|
// Register the applet update callback
|
2018-10-27 19:53:20 +00:00
|
|
|
applet_update_event = Core::System::GetInstance().CoreTiming().RegisterEvent(
|
|
|
|
"HLE Applet Update Event", AppletUpdateEvent);
|
2015-05-26 16:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown() {
|
2018-10-27 19:53:20 +00:00
|
|
|
Core::System::GetInstance().CoreTiming().RemoveEvent(applet_update_event);
|
2015-05-26 16:00:26 +00:00
|
|
|
}
|
2019-02-19 01:34:18 +00:00
|
|
|
} // namespace HLE::Applets
|