applets.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include "common/assert.h"
  6. #include "core/core.h"
  7. #include "core/frontend/applets/controller.h"
  8. #include "core/frontend/applets/error.h"
  9. #include "core/frontend/applets/general_frontend.h"
  10. #include "core/frontend/applets/profile_select.h"
  11. #include "core/frontend/applets/software_keyboard.h"
  12. #include "core/frontend/applets/web_browser.h"
  13. #include "core/hle/kernel/k_readable_event.h"
  14. #include "core/hle/kernel/k_writable_event.h"
  15. #include "core/hle/kernel/server_session.h"
  16. #include "core/hle/service/am/am.h"
  17. #include "core/hle/service/am/applet_ae.h"
  18. #include "core/hle/service/am/applet_oe.h"
  19. #include "core/hle/service/am/applets/applets.h"
  20. #include "core/hle/service/am/applets/controller.h"
  21. #include "core/hle/service/am/applets/error.h"
  22. #include "core/hle/service/am/applets/general_backend.h"
  23. #include "core/hle/service/am/applets/profile_select.h"
  24. #include "core/hle/service/am/applets/software_keyboard.h"
  25. #include "core/hle/service/am/applets/web_browser.h"
  26. #include "core/hle/service/sm/sm.h"
  27. namespace Service::AM::Applets {
  28. AppletDataBroker::AppletDataBroker(Core::System& system_, LibraryAppletMode applet_mode_)
  29. : system{system_}, applet_mode{applet_mode_}, state_changed_event{system.Kernel()},
  30. pop_out_data_event{system.Kernel()}, pop_interactive_out_data_event{system.Kernel()} {
  31. Kernel::KAutoObject::Create(std::addressof(state_changed_event));
  32. Kernel::KAutoObject::Create(std::addressof(pop_out_data_event));
  33. Kernel::KAutoObject::Create(std::addressof(pop_interactive_out_data_event));
  34. state_changed_event.Initialize("ILibraryAppletAccessor:StateChangedEvent");
  35. pop_out_data_event.Initialize("ILibraryAppletAccessor:PopDataOutEvent");
  36. pop_interactive_out_data_event.Initialize("ILibraryAppletAccessor:PopInteractiveDataOutEvent");
  37. }
  38. AppletDataBroker::~AppletDataBroker() = default;
  39. AppletDataBroker::RawChannelData AppletDataBroker::PeekDataToAppletForDebug() const {
  40. std::vector<std::vector<u8>> out_normal;
  41. for (const auto& storage : in_channel) {
  42. out_normal.push_back(storage->GetData());
  43. }
  44. std::vector<std::vector<u8>> out_interactive;
  45. for (const auto& storage : in_interactive_channel) {
  46. out_interactive.push_back(storage->GetData());
  47. }
  48. return {std::move(out_normal), std::move(out_interactive)};
  49. }
  50. std::shared_ptr<IStorage> AppletDataBroker::PopNormalDataToGame() {
  51. if (out_channel.empty())
  52. return nullptr;
  53. auto out = std::move(out_channel.front());
  54. out_channel.pop_front();
  55. pop_out_data_event.GetWritableEvent().Clear();
  56. return out;
  57. }
  58. std::shared_ptr<IStorage> AppletDataBroker::PopNormalDataToApplet() {
  59. if (in_channel.empty())
  60. return nullptr;
  61. auto out = std::move(in_channel.front());
  62. in_channel.pop_front();
  63. return out;
  64. }
  65. std::shared_ptr<IStorage> AppletDataBroker::PopInteractiveDataToGame() {
  66. if (out_interactive_channel.empty())
  67. return nullptr;
  68. auto out = std::move(out_interactive_channel.front());
  69. out_interactive_channel.pop_front();
  70. pop_interactive_out_data_event.GetWritableEvent().Clear();
  71. return out;
  72. }
  73. std::shared_ptr<IStorage> AppletDataBroker::PopInteractiveDataToApplet() {
  74. if (in_interactive_channel.empty())
  75. return nullptr;
  76. auto out = std::move(in_interactive_channel.front());
  77. in_interactive_channel.pop_front();
  78. return out;
  79. }
  80. void AppletDataBroker::PushNormalDataFromGame(std::shared_ptr<IStorage>&& storage) {
  81. in_channel.emplace_back(std::move(storage));
  82. }
  83. void AppletDataBroker::PushNormalDataFromApplet(std::shared_ptr<IStorage>&& storage) {
  84. out_channel.emplace_back(std::move(storage));
  85. pop_out_data_event.GetWritableEvent().Signal();
  86. }
  87. void AppletDataBroker::PushInteractiveDataFromGame(std::shared_ptr<IStorage>&& storage) {
  88. in_interactive_channel.emplace_back(std::move(storage));
  89. }
  90. void AppletDataBroker::PushInteractiveDataFromApplet(std::shared_ptr<IStorage>&& storage) {
  91. out_interactive_channel.emplace_back(std::move(storage));
  92. pop_interactive_out_data_event.GetWritableEvent().Signal();
  93. }
  94. void AppletDataBroker::SignalStateChanged() {
  95. state_changed_event.GetWritableEvent().Signal();
  96. switch (applet_mode) {
  97. case LibraryAppletMode::AllForeground:
  98. case LibraryAppletMode::AllForegroundInitiallyHidden: {
  99. auto applet_oe = system.ServiceManager().GetService<AppletOE>("appletOE");
  100. auto applet_ae = system.ServiceManager().GetService<AppletAE>("appletAE");
  101. if (applet_oe) {
  102. applet_oe->GetMessageQueue()->FocusStateChanged();
  103. break;
  104. }
  105. if (applet_ae) {
  106. applet_ae->GetMessageQueue()->FocusStateChanged();
  107. break;
  108. }
  109. break;
  110. }
  111. default:
  112. break;
  113. }
  114. }
  115. Kernel::KReadableEvent& AppletDataBroker::GetNormalDataEvent() {
  116. return pop_out_data_event.GetReadableEvent();
  117. }
  118. Kernel::KReadableEvent& AppletDataBroker::GetInteractiveDataEvent() {
  119. return pop_interactive_out_data_event.GetReadableEvent();
  120. }
  121. Kernel::KReadableEvent& AppletDataBroker::GetStateChangedEvent() {
  122. return state_changed_event.GetReadableEvent();
  123. }
  124. Applet::Applet(Core::System& system_, LibraryAppletMode applet_mode_)
  125. : broker{system_, applet_mode_}, applet_mode{applet_mode_} {}
  126. Applet::~Applet() = default;
  127. void Applet::Initialize() {
  128. const auto common = broker.PopNormalDataToApplet();
  129. ASSERT(common != nullptr);
  130. const auto common_data = common->GetData();
  131. ASSERT(common_data.size() >= sizeof(CommonArguments));
  132. std::memcpy(&common_args, common_data.data(), sizeof(CommonArguments));
  133. initialized = true;
  134. }
  135. AppletFrontendSet::AppletFrontendSet() = default;
  136. AppletFrontendSet::AppletFrontendSet(ControllerApplet controller_applet, ErrorApplet error_applet,
  137. ParentalControlsApplet parental_controls_applet,
  138. PhotoViewer photo_viewer_, ProfileSelect profile_select_,
  139. SoftwareKeyboard software_keyboard_, WebBrowser web_browser_)
  140. : controller{std::move(controller_applet)}, error{std::move(error_applet)},
  141. parental_controls{std::move(parental_controls_applet)},
  142. photo_viewer{std::move(photo_viewer_)}, profile_select{std::move(profile_select_)},
  143. software_keyboard{std::move(software_keyboard_)}, web_browser{std::move(web_browser_)} {}
  144. AppletFrontendSet::~AppletFrontendSet() = default;
  145. AppletFrontendSet::AppletFrontendSet(AppletFrontendSet&&) noexcept = default;
  146. AppletFrontendSet& AppletFrontendSet::operator=(AppletFrontendSet&&) noexcept = default;
  147. AppletManager::AppletManager(Core::System& system_) : system{system_} {}
  148. AppletManager::~AppletManager() = default;
  149. const AppletFrontendSet& AppletManager::GetAppletFrontendSet() const {
  150. return frontend;
  151. }
  152. void AppletManager::SetAppletFrontendSet(AppletFrontendSet set) {
  153. if (set.controller != nullptr) {
  154. frontend.controller = std::move(set.controller);
  155. }
  156. if (set.error != nullptr) {
  157. frontend.error = std::move(set.error);
  158. }
  159. if (set.parental_controls != nullptr) {
  160. frontend.parental_controls = std::move(set.parental_controls);
  161. }
  162. if (set.photo_viewer != nullptr) {
  163. frontend.photo_viewer = std::move(set.photo_viewer);
  164. }
  165. if (set.profile_select != nullptr) {
  166. frontend.profile_select = std::move(set.profile_select);
  167. }
  168. if (set.software_keyboard != nullptr) {
  169. frontend.software_keyboard = std::move(set.software_keyboard);
  170. }
  171. if (set.web_browser != nullptr) {
  172. frontend.web_browser = std::move(set.web_browser);
  173. }
  174. }
  175. void AppletManager::SetDefaultAppletFrontendSet() {
  176. ClearAll();
  177. SetDefaultAppletsIfMissing();
  178. }
  179. void AppletManager::SetDefaultAppletsIfMissing() {
  180. if (frontend.controller == nullptr) {
  181. frontend.controller =
  182. std::make_unique<Core::Frontend::DefaultControllerApplet>(system.ServiceManager());
  183. }
  184. if (frontend.error == nullptr) {
  185. frontend.error = std::make_unique<Core::Frontend::DefaultErrorApplet>();
  186. }
  187. if (frontend.parental_controls == nullptr) {
  188. frontend.parental_controls =
  189. std::make_unique<Core::Frontend::DefaultParentalControlsApplet>();
  190. }
  191. if (frontend.photo_viewer == nullptr) {
  192. frontend.photo_viewer = std::make_unique<Core::Frontend::DefaultPhotoViewerApplet>();
  193. }
  194. if (frontend.profile_select == nullptr) {
  195. frontend.profile_select = std::make_unique<Core::Frontend::DefaultProfileSelectApplet>();
  196. }
  197. if (frontend.software_keyboard == nullptr) {
  198. frontend.software_keyboard =
  199. std::make_unique<Core::Frontend::DefaultSoftwareKeyboardApplet>();
  200. }
  201. if (frontend.web_browser == nullptr) {
  202. frontend.web_browser = std::make_unique<Core::Frontend::DefaultWebBrowserApplet>();
  203. }
  204. }
  205. void AppletManager::ClearAll() {
  206. frontend = {};
  207. }
  208. std::shared_ptr<Applet> AppletManager::GetApplet(AppletId id, LibraryAppletMode mode) const {
  209. switch (id) {
  210. case AppletId::Auth:
  211. return std::make_shared<Auth>(system, mode, *frontend.parental_controls);
  212. case AppletId::Controller:
  213. return std::make_shared<Controller>(system, mode, *frontend.controller);
  214. case AppletId::Error:
  215. return std::make_shared<Error>(system, mode, *frontend.error);
  216. case AppletId::ProfileSelect:
  217. return std::make_shared<ProfileSelect>(system, mode, *frontend.profile_select);
  218. case AppletId::SoftwareKeyboard:
  219. return std::make_shared<SoftwareKeyboard>(system, mode, *frontend.software_keyboard);
  220. case AppletId::Web:
  221. case AppletId::Shop:
  222. case AppletId::OfflineWeb:
  223. case AppletId::LoginShare:
  224. case AppletId::WebAuth:
  225. return std::make_shared<WebBrowser>(system, mode, *frontend.web_browser);
  226. case AppletId::PhotoViewer:
  227. return std::make_shared<PhotoViewer>(system, mode, *frontend.photo_viewer);
  228. default:
  229. UNIMPLEMENTED_MSG(
  230. "No backend implementation exists for applet_id={:02X}! Falling back to stub applet.",
  231. static_cast<u8>(id));
  232. return std::make_shared<StubApplet>(system, id, mode);
  233. }
  234. }
  235. } // namespace Service::AM::Applets