applet.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstddef>
  5. #include <memory>
  6. #include <type_traits>
  7. #include <unordered_map>
  8. #include "common/assert.h"
  9. #include "common/common_types.h"
  10. #include "core/core_timing.h"
  11. #include "core/hle/applets/applet.h"
  12. #include "core/hle/applets/erreula.h"
  13. #include "core/hle/applets/mii_selector.h"
  14. #include "core/hle/applets/mint.h"
  15. #include "core/hle/applets/swkbd.h"
  16. #include "core/hle/result.h"
  17. #include "core/hle/service/apt/apt.h"
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////
  19. // Specializes std::hash for AppletId, so that we can use it in std::unordered_map.
  20. // Workaround for libstdc++ bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60970
  21. namespace std {
  22. template <>
  23. struct hash<Service::APT::AppletId> {
  24. typedef Service::APT::AppletId argument_type;
  25. typedef std::size_t result_type;
  26. result_type operator()(const argument_type& id_code) const {
  27. typedef std::underlying_type<argument_type>::type Type;
  28. return std::hash<Type>()(static_cast<Type>(id_code));
  29. }
  30. };
  31. }
  32. namespace HLE {
  33. namespace Applets {
  34. static std::unordered_map<Service::APT::AppletId, std::shared_ptr<Applet>> applets;
  35. static u32 applet_update_event =
  36. -1; ///< The CoreTiming event identifier for the Applet update callback.
  37. /// The interval at which the Applet update callback will be called, 16.6ms
  38. static const u64 applet_update_interval_us = 16666;
  39. ResultCode Applet::Create(Service::APT::AppletId id) {
  40. switch (id) {
  41. case Service::APT::AppletId::SoftwareKeyboard1:
  42. case Service::APT::AppletId::SoftwareKeyboard2:
  43. applets[id] = std::make_shared<SoftwareKeyboard>(id);
  44. break;
  45. case Service::APT::AppletId::Ed1:
  46. case Service::APT::AppletId::Ed2:
  47. applets[id] = std::make_shared<MiiSelector>(id);
  48. break;
  49. case Service::APT::AppletId::Error:
  50. case Service::APT::AppletId::Error2:
  51. applets[id] = std::make_shared<ErrEula>(id);
  52. break;
  53. case Service::APT::AppletId::Mint:
  54. case Service::APT::AppletId::Mint2:
  55. applets[id] = std::make_shared<Mint>(id);
  56. break;
  57. default:
  58. LOG_ERROR(Service_APT, "Could not create applet %u", id);
  59. // TODO(Subv): Find the right error code
  60. return ResultCode(ErrorDescription::NotFound, ErrorModule::Applet,
  61. ErrorSummary::NotSupported, ErrorLevel::Permanent);
  62. }
  63. return RESULT_SUCCESS;
  64. }
  65. std::shared_ptr<Applet> Applet::Get(Service::APT::AppletId id) {
  66. auto itr = applets.find(id);
  67. if (itr != applets.end())
  68. return itr->second;
  69. return nullptr;
  70. }
  71. /// Handles updating the current Applet every time it's called.
  72. static void AppletUpdateEvent(u64 applet_id, int cycles_late) {
  73. Service::APT::AppletId id = static_cast<Service::APT::AppletId>(applet_id);
  74. std::shared_ptr<Applet> applet = Applet::Get(id);
  75. ASSERT_MSG(applet != nullptr, "Applet doesn't exist! applet_id=%08X", id);
  76. applet->Update();
  77. // If the applet is still running after the last update, reschedule the event
  78. if (applet->IsRunning()) {
  79. CoreTiming::ScheduleEvent(usToCycles(applet_update_interval_us) - cycles_late,
  80. applet_update_event, applet_id);
  81. } else {
  82. // Otherwise the applet has terminated, in which case we should clean it up
  83. applets[id] = nullptr;
  84. }
  85. }
  86. ResultCode Applet::Start(const Service::APT::AppletStartupParameter& parameter) {
  87. ResultCode result = StartImpl(parameter);
  88. if (result.IsError())
  89. return result;
  90. // Schedule the update event
  91. CoreTiming::ScheduleEvent(usToCycles(applet_update_interval_us), applet_update_event,
  92. static_cast<u64>(id));
  93. return result;
  94. }
  95. bool Applet::IsRunning() const {
  96. return is_running;
  97. }
  98. bool IsLibraryAppletRunning() {
  99. // Check the applets map for instances of any applet
  100. for (auto itr = applets.begin(); itr != applets.end(); ++itr)
  101. if (itr->second != nullptr)
  102. return true;
  103. return false;
  104. }
  105. void Init() {
  106. // Register the applet update callback
  107. applet_update_event = CoreTiming::RegisterEvent("HLE Applet Update Event", AppletUpdateEvent);
  108. }
  109. void Shutdown() {
  110. CoreTiming::RemoveEvent(applet_update_event);
  111. }
  112. }
  113. } // namespace