applets.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <cstring>
  4. #include "common/assert.h"
  5. #include "core/core.h"
  6. #include "core/frontend/applets/cabinet.h"
  7. #include "core/frontend/applets/controller.h"
  8. #include "core/frontend/applets/error.h"
  9. #include "core/frontend/applets/general.h"
  10. #include "core/frontend/applets/mii_edit.h"
  11. #include "core/frontend/applets/profile_select.h"
  12. #include "core/frontend/applets/software_keyboard.h"
  13. #include "core/frontend/applets/web_browser.h"
  14. #include "core/hle/kernel/k_event.h"
  15. #include "core/hle/service/am/am.h"
  16. #include "core/hle/service/am/applet_ae.h"
  17. #include "core/hle/service/am/applet_manager.h"
  18. #include "core/hle/service/am/applet_message_queue.h"
  19. #include "core/hle/service/am/applet_oe.h"
  20. #include "core/hle/service/am/frontend/applet_cabinet.h"
  21. #include "core/hle/service/am/frontend/applet_controller.h"
  22. #include "core/hle/service/am/frontend/applet_error.h"
  23. #include "core/hle/service/am/frontend/applet_general.h"
  24. #include "core/hle/service/am/frontend/applet_mii_edit.h"
  25. #include "core/hle/service/am/frontend/applet_profile_select.h"
  26. #include "core/hle/service/am/frontend/applet_software_keyboard.h"
  27. #include "core/hle/service/am/frontend/applet_web_browser.h"
  28. #include "core/hle/service/am/frontend/applets.h"
  29. #include "core/hle/service/am/storage.h"
  30. #include "core/hle/service/sm/sm.h"
  31. namespace Service::AM::Frontend {
  32. AppletDataBroker::AppletDataBroker(Core::System& system_, LibraryAppletMode applet_mode_)
  33. : system{system_}, applet_mode{applet_mode_},
  34. service_context{system, "ILibraryAppletAccessor"} {
  35. state_changed_event = service_context.CreateEvent("ILibraryAppletAccessor:StateChangedEvent");
  36. pop_out_data_event = service_context.CreateEvent("ILibraryAppletAccessor:PopDataOutEvent");
  37. pop_interactive_out_data_event =
  38. service_context.CreateEvent("ILibraryAppletAccessor:PopInteractiveDataOutEvent");
  39. }
  40. AppletDataBroker::~AppletDataBroker() {
  41. service_context.CloseEvent(state_changed_event);
  42. service_context.CloseEvent(pop_out_data_event);
  43. service_context.CloseEvent(pop_interactive_out_data_event);
  44. }
  45. AppletDataBroker::RawChannelData AppletDataBroker::PeekDataToAppletForDebug() const {
  46. std::vector<std::vector<u8>> out_normal;
  47. for (const auto& storage : in_channel) {
  48. out_normal.push_back(storage->GetData());
  49. }
  50. std::vector<std::vector<u8>> out_interactive;
  51. for (const auto& storage : in_interactive_channel) {
  52. out_interactive.push_back(storage->GetData());
  53. }
  54. return {std::move(out_normal), std::move(out_interactive)};
  55. }
  56. std::shared_ptr<IStorage> AppletDataBroker::PopNormalDataToGame() {
  57. if (out_channel.empty())
  58. return nullptr;
  59. auto out = std::move(out_channel.front());
  60. out_channel.pop_front();
  61. pop_out_data_event->Clear();
  62. return out;
  63. }
  64. std::shared_ptr<IStorage> AppletDataBroker::PopNormalDataToApplet() {
  65. if (in_channel.empty())
  66. return nullptr;
  67. auto out = std::move(in_channel.front());
  68. in_channel.pop_front();
  69. return out;
  70. }
  71. std::shared_ptr<IStorage> AppletDataBroker::PopInteractiveDataToGame() {
  72. if (out_interactive_channel.empty())
  73. return nullptr;
  74. auto out = std::move(out_interactive_channel.front());
  75. out_interactive_channel.pop_front();
  76. pop_interactive_out_data_event->Clear();
  77. return out;
  78. }
  79. std::shared_ptr<IStorage> AppletDataBroker::PopInteractiveDataToApplet() {
  80. if (in_interactive_channel.empty())
  81. return nullptr;
  82. auto out = std::move(in_interactive_channel.front());
  83. in_interactive_channel.pop_front();
  84. return out;
  85. }
  86. void AppletDataBroker::PushNormalDataFromGame(std::shared_ptr<IStorage>&& storage) {
  87. in_channel.emplace_back(std::move(storage));
  88. }
  89. void AppletDataBroker::PushNormalDataFromApplet(std::shared_ptr<IStorage>&& storage) {
  90. out_channel.emplace_back(std::move(storage));
  91. pop_out_data_event->Signal();
  92. }
  93. void AppletDataBroker::PushInteractiveDataFromGame(std::shared_ptr<IStorage>&& storage) {
  94. in_interactive_channel.emplace_back(std::move(storage));
  95. }
  96. void AppletDataBroker::PushInteractiveDataFromApplet(std::shared_ptr<IStorage>&& storage) {
  97. out_interactive_channel.emplace_back(std::move(storage));
  98. pop_interactive_out_data_event->Signal();
  99. }
  100. void AppletDataBroker::SignalStateChanged() {
  101. state_changed_event->Signal();
  102. // TODO proper window management
  103. switch (applet_mode) {
  104. case LibraryAppletMode::AllForeground:
  105. case LibraryAppletMode::AllForegroundInitiallyHidden: {
  106. system.GetAppletManager().FocusStateChanged();
  107. break;
  108. }
  109. default:
  110. break;
  111. }
  112. }
  113. Kernel::KReadableEvent& AppletDataBroker::GetNormalDataEvent() {
  114. return pop_out_data_event->GetReadableEvent();
  115. }
  116. Kernel::KReadableEvent& AppletDataBroker::GetInteractiveDataEvent() {
  117. return pop_interactive_out_data_event->GetReadableEvent();
  118. }
  119. Kernel::KReadableEvent& AppletDataBroker::GetStateChangedEvent() {
  120. return state_changed_event->GetReadableEvent();
  121. }
  122. FrontendApplet::FrontendApplet(Core::System& system_, LibraryAppletMode applet_mode_)
  123. : broker{system_, applet_mode_}, applet_mode{applet_mode_} {}
  124. FrontendApplet::~FrontendApplet() = default;
  125. void FrontendApplet::Initialize() {
  126. const auto common = broker.PopNormalDataToApplet();
  127. ASSERT(common != nullptr);
  128. const auto common_data = common->GetData();
  129. ASSERT(common_data.size() >= sizeof(CommonArguments));
  130. std::memcpy(&common_args, common_data.data(), sizeof(CommonArguments));
  131. initialized = true;
  132. }
  133. FrontendAppletSet::FrontendAppletSet() = default;
  134. FrontendAppletSet::FrontendAppletSet(CabinetApplet cabinet_applet,
  135. ControllerApplet controller_applet, ErrorApplet error_applet,
  136. MiiEdit mii_edit_,
  137. ParentalControlsApplet parental_controls_applet,
  138. PhotoViewer photo_viewer_, ProfileSelect profile_select_,
  139. SoftwareKeyboard software_keyboard_, WebBrowser web_browser_)
  140. : cabinet{std::move(cabinet_applet)}, controller{std::move(controller_applet)},
  141. error{std::move(error_applet)}, mii_edit{std::move(mii_edit_)},
  142. parental_controls{std::move(parental_controls_applet)},
  143. photo_viewer{std::move(photo_viewer_)}, profile_select{std::move(profile_select_)},
  144. software_keyboard{std::move(software_keyboard_)}, web_browser{std::move(web_browser_)} {}
  145. FrontendAppletSet::~FrontendAppletSet() = default;
  146. FrontendAppletSet::FrontendAppletSet(FrontendAppletSet&&) noexcept = default;
  147. FrontendAppletSet& FrontendAppletSet::operator=(FrontendAppletSet&&) noexcept = default;
  148. FrontendAppletHolder::FrontendAppletHolder(Core::System& system_) : system{system_} {}
  149. FrontendAppletHolder::~FrontendAppletHolder() = default;
  150. const FrontendAppletSet& FrontendAppletHolder::GetFrontendAppletSet() const {
  151. return frontend;
  152. }
  153. NFP::CabinetMode FrontendAppletHolder::GetCabinetMode() const {
  154. return cabinet_mode;
  155. }
  156. AppletId FrontendAppletHolder::GetCurrentAppletId() const {
  157. return current_applet_id;
  158. }
  159. void FrontendAppletHolder::SetFrontendAppletSet(FrontendAppletSet set) {
  160. if (set.cabinet != nullptr) {
  161. frontend.cabinet = std::move(set.cabinet);
  162. }
  163. if (set.controller != nullptr) {
  164. frontend.controller = std::move(set.controller);
  165. }
  166. if (set.error != nullptr) {
  167. frontend.error = std::move(set.error);
  168. }
  169. if (set.mii_edit != nullptr) {
  170. frontend.mii_edit = std::move(set.mii_edit);
  171. }
  172. if (set.parental_controls != nullptr) {
  173. frontend.parental_controls = std::move(set.parental_controls);
  174. }
  175. if (set.photo_viewer != nullptr) {
  176. frontend.photo_viewer = std::move(set.photo_viewer);
  177. }
  178. if (set.profile_select != nullptr) {
  179. frontend.profile_select = std::move(set.profile_select);
  180. }
  181. if (set.software_keyboard != nullptr) {
  182. frontend.software_keyboard = std::move(set.software_keyboard);
  183. }
  184. if (set.web_browser != nullptr) {
  185. frontend.web_browser = std::move(set.web_browser);
  186. }
  187. }
  188. void FrontendAppletHolder::SetCabinetMode(NFP::CabinetMode mode) {
  189. cabinet_mode = mode;
  190. }
  191. void FrontendAppletHolder::SetCurrentAppletId(AppletId applet_id) {
  192. current_applet_id = applet_id;
  193. }
  194. void FrontendAppletHolder::SetDefaultAppletsIfMissing() {
  195. if (frontend.cabinet == nullptr) {
  196. frontend.cabinet = std::make_unique<Core::Frontend::DefaultCabinetApplet>();
  197. }
  198. if (frontend.controller == nullptr) {
  199. frontend.controller =
  200. std::make_unique<Core::Frontend::DefaultControllerApplet>(system.HIDCore());
  201. }
  202. if (frontend.error == nullptr) {
  203. frontend.error = std::make_unique<Core::Frontend::DefaultErrorApplet>();
  204. }
  205. if (frontend.mii_edit == nullptr) {
  206. frontend.mii_edit = std::make_unique<Core::Frontend::DefaultMiiEditApplet>();
  207. }
  208. if (frontend.parental_controls == nullptr) {
  209. frontend.parental_controls =
  210. std::make_unique<Core::Frontend::DefaultParentalControlsApplet>();
  211. }
  212. if (frontend.photo_viewer == nullptr) {
  213. frontend.photo_viewer = std::make_unique<Core::Frontend::DefaultPhotoViewerApplet>();
  214. }
  215. if (frontend.profile_select == nullptr) {
  216. frontend.profile_select = std::make_unique<Core::Frontend::DefaultProfileSelectApplet>();
  217. }
  218. if (frontend.software_keyboard == nullptr) {
  219. frontend.software_keyboard =
  220. std::make_unique<Core::Frontend::DefaultSoftwareKeyboardApplet>();
  221. }
  222. if (frontend.web_browser == nullptr) {
  223. frontend.web_browser = std::make_unique<Core::Frontend::DefaultWebBrowserApplet>();
  224. }
  225. }
  226. void FrontendAppletHolder::ClearAll() {
  227. frontend = {};
  228. }
  229. std::shared_ptr<FrontendApplet> FrontendAppletHolder::GetApplet(AppletId id,
  230. LibraryAppletMode mode) const {
  231. switch (id) {
  232. case AppletId::Auth:
  233. return std::make_shared<Auth>(system, mode, *frontend.parental_controls);
  234. case AppletId::Cabinet:
  235. return std::make_shared<Cabinet>(system, mode, *frontend.cabinet);
  236. case AppletId::Controller:
  237. return std::make_shared<Controller>(system, mode, *frontend.controller);
  238. case AppletId::Error:
  239. return std::make_shared<Error>(system, mode, *frontend.error);
  240. case AppletId::ProfileSelect:
  241. return std::make_shared<ProfileSelect>(system, mode, *frontend.profile_select);
  242. case AppletId::SoftwareKeyboard:
  243. return std::make_shared<SoftwareKeyboard>(system, mode, *frontend.software_keyboard);
  244. case AppletId::MiiEdit:
  245. return std::make_shared<MiiEdit>(system, mode, *frontend.mii_edit);
  246. case AppletId::Web:
  247. case AppletId::Shop:
  248. case AppletId::OfflineWeb:
  249. case AppletId::LoginShare:
  250. case AppletId::WebAuth:
  251. return std::make_shared<WebBrowser>(system, mode, *frontend.web_browser);
  252. case AppletId::PhotoViewer:
  253. return std::make_shared<PhotoViewer>(system, mode, *frontend.photo_viewer);
  254. default:
  255. UNIMPLEMENTED_MSG(
  256. "No backend implementation exists for applet_id={:02X}! Falling back to stub applet.",
  257. static_cast<u8>(id));
  258. return std::make_shared<StubApplet>(system, id, mode);
  259. }
  260. }
  261. } // namespace Service::AM::Frontend