2015-05-26 06:30:20 +02:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-07-12 23:10:37 +02:00
|
|
|
#include <cstddef>
|
|
|
|
#include <memory>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_map>
|
2015-05-26 06:30:20 +02:00
|
|
|
#include "common/assert.h"
|
2015-07-12 23:10:37 +02:00
|
|
|
#include "common/common_types.h"
|
2015-05-26 18:00:26 +02:00
|
|
|
#include "core/core_timing.h"
|
2016-09-21 08:52:38 +02:00
|
|
|
#include "core/hle/applets/applet.h"
|
2016-03-30 14:52:59 +02:00
|
|
|
#include "core/hle/applets/erreula.h"
|
2015-12-04 22:05:23 +01:00
|
|
|
#include "core/hle/applets/mii_selector.h"
|
2017-01-31 10:16:58 +01:00
|
|
|
#include "core/hle/applets/mint.h"
|
2015-05-26 06:30:20 +02:00
|
|
|
#include "core/hle/applets/swkbd.h"
|
2015-07-12 23:10:37 +02:00
|
|
|
#include "core/hle/result.h"
|
|
|
|
#include "core/hle/service/apt/apt.h"
|
2015-05-26 06:30:20 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-05-26 18:00:26 +02:00
|
|
|
// Specializes std::hash for AppletId, so that we can use it in std::unordered_map.
|
|
|
|
// Workaround for libstdc++ bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60970
|
|
|
|
namespace std {
|
2016-09-18 02:38:01 +02:00
|
|
|
template <>
|
|
|
|
struct hash<Service::APT::AppletId> {
|
|
|
|
typedef Service::APT::AppletId argument_type;
|
|
|
|
typedef std::size_t result_type;
|
|
|
|
|
|
|
|
result_type operator()(const argument_type& id_code) const {
|
|
|
|
typedef std::underlying_type<argument_type>::type Type;
|
|
|
|
return std::hash<Type>()(static_cast<Type>(id_code));
|
|
|
|
}
|
|
|
|
};
|
2015-05-26 18:00:26 +02:00
|
|
|
}
|
|
|
|
|
2015-05-26 06:30:20 +02:00
|
|
|
namespace HLE {
|
|
|
|
namespace Applets {
|
|
|
|
|
|
|
|
static std::unordered_map<Service::APT::AppletId, std::shared_ptr<Applet>> applets;
|
2017-11-25 14:56:57 +01:00
|
|
|
/// The CoreTiming event identifier for the Applet update callback.
|
|
|
|
static CoreTiming::EventType* applet_update_event = nullptr;
|
2015-05-27 22:21:06 +02: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 06:30:20 +02:00
|
|
|
|
|
|
|
ResultCode Applet::Create(Service::APT::AppletId id) {
|
|
|
|
switch (id) {
|
|
|
|
case Service::APT::AppletId::SoftwareKeyboard1:
|
|
|
|
case Service::APT::AppletId::SoftwareKeyboard2:
|
|
|
|
applets[id] = std::make_shared<SoftwareKeyboard>(id);
|
|
|
|
break;
|
2015-12-04 22:05:23 +01:00
|
|
|
case Service::APT::AppletId::Ed1:
|
|
|
|
case Service::APT::AppletId::Ed2:
|
|
|
|
applets[id] = std::make_shared<MiiSelector>(id);
|
|
|
|
break;
|
2016-03-30 14:52:59 +02:00
|
|
|
case Service::APT::AppletId::Error:
|
|
|
|
case Service::APT::AppletId::Error2:
|
|
|
|
applets[id] = std::make_shared<ErrEula>(id);
|
|
|
|
break;
|
2017-01-31 10:16:58 +01:00
|
|
|
case Service::APT::AppletId::Mint:
|
|
|
|
case Service::APT::AppletId::Mint2:
|
|
|
|
applets[id] = std::make_shared<Mint>(id);
|
|
|
|
break;
|
2015-05-26 06:30:20 +02:00
|
|
|
default:
|
2017-11-01 11:19:51 +01:00
|
|
|
LOG_ERROR(Service_APT, "Could not create applet %u", static_cast<u32>(id));
|
2015-05-26 06:30:20 +02:00
|
|
|
// TODO(Subv): Find the right error code
|
2016-09-18 02:38:01 +02:00
|
|
|
return ResultCode(ErrorDescription::NotFound, ErrorModule::Applet,
|
|
|
|
ErrorSummary::NotSupported, ErrorLevel::Permanent);
|
2015-05-26 06:30:20 +02: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 18:00:26 +02:00
|
|
|
/// Handles updating the current Applet every time it's called.
|
2015-05-27 22:21:06 +02:00
|
|
|
static void AppletUpdateEvent(u64 applet_id, int cycles_late) {
|
|
|
|
Service::APT::AppletId id = static_cast<Service::APT::AppletId>(applet_id);
|
|
|
|
std::shared_ptr<Applet> applet = Applet::Get(id);
|
2017-11-01 11:19:51 +01:00
|
|
|
ASSERT_MSG(applet != nullptr, "Applet doesn't exist! applet_id=%08X", static_cast<u32>(id));
|
2015-05-26 18:00:26 +02:00
|
|
|
|
2015-05-27 22:21:06 +02:00
|
|
|
applet->Update();
|
|
|
|
|
|
|
|
// If the applet is still running after the last update, reschedule the event
|
|
|
|
if (applet->IsRunning()) {
|
|
|
|
CoreTiming::ScheduleEvent(usToCycles(applet_update_interval_us) - cycles_late,
|
2016-09-18 02:38:01 +02:00
|
|
|
applet_update_event, applet_id);
|
2015-05-27 22:21:06 +02:00
|
|
|
} else {
|
|
|
|
// Otherwise the applet has terminated, in which case we should clean it up
|
|
|
|
applets[id] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode Applet::Start(const Service::APT::AppletStartupParameter& parameter) {
|
|
|
|
ResultCode result = StartImpl(parameter);
|
|
|
|
if (result.IsError())
|
|
|
|
return result;
|
|
|
|
// Schedule the update event
|
2016-09-18 02:38:01 +02:00
|
|
|
CoreTiming::ScheduleEvent(usToCycles(applet_update_interval_us), applet_update_event,
|
|
|
|
static_cast<u64>(id));
|
2015-05-27 22:21:06 +02:00
|
|
|
return result;
|
2015-05-26 18:00:26 +02:00
|
|
|
}
|
|
|
|
|
2016-12-07 23:11:39 +01:00
|
|
|
bool Applet::IsRunning() const {
|
|
|
|
return is_running;
|
|
|
|
}
|
|
|
|
|
2015-07-24 01:52:57 +02:00
|
|
|
bool IsLibraryAppletRunning() {
|
|
|
|
// Check the applets map for instances of any applet
|
2015-07-24 04:09:43 +02:00
|
|
|
for (auto itr = applets.begin(); itr != applets.end(); ++itr)
|
2015-07-24 01:52:57 +02:00
|
|
|
if (itr->second != nullptr)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-26 18:00:26 +02:00
|
|
|
void Init() {
|
2015-05-27 22:21:06 +02:00
|
|
|
// Register the applet update callback
|
2015-05-26 18:00:26 +02:00
|
|
|
applet_update_event = CoreTiming::RegisterEvent("HLE Applet Update Event", AppletUpdateEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown() {
|
2015-07-24 01:52:57 +02:00
|
|
|
CoreTiming::RemoveEvent(applet_update_event);
|
2015-05-26 18:00:26 +02:00
|
|
|
}
|
2015-05-26 06:30:20 +02:00
|
|
|
}
|
|
|
|
} // namespace
|