applet.cpp 4.0 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 = -1; ///< The CoreTiming event identifier for the Applet update callback.
  35. /// The interval at which the Applet update callback will be called, 16.6ms
  36. static const u64 applet_update_interval_us = 16666;
  37. ResultCode Applet::Create(Service::APT::AppletId id) {
  38. switch (id) {
  39. case Service::APT::AppletId::SoftwareKeyboard1:
  40. case Service::APT::AppletId::SoftwareKeyboard2:
  41. applets[id] = std::make_shared<SoftwareKeyboard>(id);
  42. break;
  43. case Service::APT::AppletId::Ed1:
  44. case Service::APT::AppletId::Ed2:
  45. applets[id] = std::make_shared<MiiSelector>(id);
  46. break;
  47. case Service::APT::AppletId::Error:
  48. case Service::APT::AppletId::Error2:
  49. applets[id] = std::make_shared<ErrEula>(id);
  50. break;
  51. default:
  52. LOG_ERROR(Service_APT, "Could not create applet %u", id);
  53. // TODO(Subv): Find the right error code
  54. return ResultCode(ErrorDescription::NotFound, ErrorModule::Applet, ErrorSummary::NotSupported, ErrorLevel::Permanent);
  55. }
  56. return RESULT_SUCCESS;
  57. }
  58. std::shared_ptr<Applet> Applet::Get(Service::APT::AppletId id) {
  59. auto itr = applets.find(id);
  60. if (itr != applets.end())
  61. return itr->second;
  62. return nullptr;
  63. }
  64. /// Handles updating the current Applet every time it's called.
  65. static void AppletUpdateEvent(u64 applet_id, int cycles_late) {
  66. Service::APT::AppletId id = static_cast<Service::APT::AppletId>(applet_id);
  67. std::shared_ptr<Applet> applet = Applet::Get(id);
  68. ASSERT_MSG(applet != nullptr, "Applet doesn't exist! applet_id=%08X", id);
  69. applet->Update();
  70. // If the applet is still running after the last update, reschedule the event
  71. if (applet->IsRunning()) {
  72. CoreTiming::ScheduleEvent(usToCycles(applet_update_interval_us) - cycles_late,
  73. applet_update_event, applet_id);
  74. } else {
  75. // Otherwise the applet has terminated, in which case we should clean it up
  76. applets[id] = nullptr;
  77. }
  78. }
  79. ResultCode Applet::Start(const Service::APT::AppletStartupParameter& parameter) {
  80. ResultCode result = StartImpl(parameter);
  81. if (result.IsError())
  82. return result;
  83. // Schedule the update event
  84. CoreTiming::ScheduleEvent(usToCycles(applet_update_interval_us), applet_update_event, static_cast<u64>(id));
  85. return result;
  86. }
  87. bool IsLibraryAppletRunning() {
  88. // Check the applets map for instances of any applet
  89. for (auto itr = applets.begin(); itr != applets.end(); ++itr)
  90. if (itr->second != nullptr)
  91. return true;
  92. return false;
  93. }
  94. void Init() {
  95. // Register the applet update callback
  96. applet_update_event = CoreTiming::RegisterEvent("HLE Applet Update Event", AppletUpdateEvent);
  97. }
  98. void Shutdown() {
  99. CoreTiming::RemoveEvent(applet_update_event);
  100. }
  101. }
  102. } // namespace