applet.cpp 4.1 KB

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