1 // Copyright 2016 Citra Emulator Project
2 // Licensed under GPLv2 or any later version
3 // Refer to the license.txt file included.
4 
5 #include <cstring>
6 #include <string>
7 #include <boost/crc.hpp>
8 #include "common/assert.h"
9 #include "common/logging/log.h"
10 #include "common/string_util.h"
11 #include "core/core.h"
12 #include "core/frontend/applets/mii_selector.h"
13 #include "core/hle/applets/mii_selector.h"
14 #include "core/hle/kernel/kernel.h"
15 #include "core/hle/kernel/shared_memory.h"
16 #include "core/hle/result.h"
17 
18 ////////////////////////////////////////////////////////////////////////////////////////////////////
19 
20 namespace HLE::Applets {
21 
ReceiveParameter(const Service::APT::MessageParameter & parameter)22 ResultCode MiiSelector::ReceiveParameter(const Service::APT::MessageParameter& parameter) {
23     if (parameter.signal != Service::APT::SignalType::Request) {
24         LOG_ERROR(Service_APT, "unsupported signal {}", parameter.signal);
25         UNIMPLEMENTED();
26         // TODO(Subv): Find the right error code
27         return ResultCode(-1);
28     }
29 
30     // The LibAppJustStarted message contains a buffer with the size of the framebuffer shared
31     // memory.
32     // Create the SharedMemory that will hold the framebuffer data
33     Service::APT::CaptureBufferInfo capture_info;
34     ASSERT(sizeof(capture_info) == parameter.buffer.size());
35 
36     memcpy(&capture_info, parameter.buffer.data(), sizeof(capture_info));
37 
38     using Kernel::MemoryPermission;
39     // Create a SharedMemory that directly points to this heap block.
40     framebuffer_memory = Core::System::GetInstance().Kernel().CreateSharedMemoryForApplet(
41         0, capture_info.size, MemoryPermission::ReadWrite, MemoryPermission::ReadWrite,
42         "MiiSelector Memory");
43 
44     // Send the response message with the newly created SharedMemory
45     Service::APT::MessageParameter result;
46     result.signal = Service::APT::SignalType::Response;
47     result.buffer.clear();
48     result.destination_id = Service::APT::AppletId::Application;
49     result.sender_id = id;
50     result.object = framebuffer_memory;
51 
52     SendParameter(result);
53     return RESULT_SUCCESS;
54 }
55 
StartImpl(const Service::APT::AppletStartupParameter & parameter)56 ResultCode MiiSelector::StartImpl(const Service::APT::AppletStartupParameter& parameter) {
57     ASSERT_MSG(parameter.buffer.size() == sizeof(config),
58                "The size of the parameter (MiiConfig) is wrong");
59 
60     memcpy(&config, parameter.buffer.data(), parameter.buffer.size());
61 
62     using namespace Frontend;
63     frontend_applet = Core::System::GetInstance().GetMiiSelector();
64     ASSERT(frontend_applet);
65 
66     MiiSelectorConfig frontend_config = ToFrontendConfig(config);
67     frontend_applet->Setup(frontend_config);
68 
69     is_running = true;
70     return RESULT_SUCCESS;
71 }
72 
Update()73 void MiiSelector::Update() {
74     using namespace Frontend;
75     const MiiSelectorData& data = frontend_applet->ReceiveData();
76     result.return_code = data.return_code;
77     result.selected_mii_data = data.mii;
78     // Calculate the checksum of the selected Mii, see https://www.3dbrew.org/wiki/Mii#Checksum
79     result.mii_data_checksum = boost::crc<16, 0x1021, 0, 0, false, false>(
80         &result.selected_mii_data, sizeof(HLE::Applets::MiiData) + sizeof(result.unknown1));
81     result.selected_guest_mii_index = 0xFFFFFFFF;
82 
83     // TODO(Subv): We're finalizing the applet immediately after it's started,
84     // but we should defer this call until after all the input has been collected.
85     Finalize();
86 }
87 
Finalize()88 void MiiSelector::Finalize() {
89     // Let the application know that we're closing
90     Service::APT::MessageParameter message;
91     message.buffer.resize(sizeof(MiiResult));
92     std::memcpy(message.buffer.data(), &result, message.buffer.size());
93     message.signal = Service::APT::SignalType::WakeupByExit;
94     message.destination_id = Service::APT::AppletId::Application;
95     message.sender_id = id;
96     SendParameter(message);
97 
98     is_running = false;
99 }
100 
GetStandardMiiResult()101 MiiResult MiiSelector::GetStandardMiiResult() {
102     // This data was obtained by writing the returned buffer in AppletManager::GlanceParameter of
103     // the LLEd Mii picker of version system version 11.8.0 to a file and then matching the values
104     // to the members of the MiiResult struct
105     MiiData mii_data;
106     mii_data.mii_id = 0x03001030;
107     mii_data.system_id = 0xD285B6B300C8850A;
108     mii_data.specialness_and_creation_date = 0x98391EE4;
109     mii_data.creator_mac = {0x40, 0xF4, 0x07, 0xB7, 0x37, 0x10};
110     mii_data.padding = 0x0;
111     mii_data.mii_information = 0xA600;
112     mii_data.mii_name = {'C', 'i', 't', 'r', 'a', 0x0, 0x0, 0x0, 0x0, 0x0};
113     mii_data.width_height = 0x4040;
114     mii_data.appearance_bits1.raw = 0x0;
115     mii_data.appearance_bits2.raw = 0x0;
116     mii_data.hair_style = 0x21;
117     mii_data.appearance_bits3.hair_color.Assign(0x1);
118     mii_data.appearance_bits3.flip_hair.Assign(0x0);
119     mii_data.unknown1 = 0x02684418;
120     mii_data.appearance_bits4.eyebrow_style.Assign(0x6);
121     mii_data.appearance_bits4.eyebrow_color.Assign(0x1);
122     mii_data.appearance_bits5.eyebrow_scale.Assign(0x4);
123     mii_data.appearance_bits5.eyebrow_yscale.Assign(0x3);
124     mii_data.appearance_bits6 = 0x4614;
125     mii_data.unknown2 = 0x81121768;
126     mii_data.allow_copying = 0x0D;
127     mii_data.unknown3 = {0x0, 0x0, 0x29, 0x0, 0x52, 0x48, 0x50};
128     mii_data.author_name = {'f', 'l', 'T', 'o', 'b', 'i', 0x0, 0x0, 0x0, 0x0};
129 
130     MiiResult result;
131     result.return_code = 0x0;
132     result.is_guest_mii_selected = 0x0;
133     result.selected_guest_mii_index = 0xFFFFFFFF;
134     result.selected_mii_data = mii_data;
135     result.unknown1 = 0x0;
136     result.mii_data_checksum = 0x056C;
137     result.guest_mii_name.fill(0x0);
138 
139     return result;
140 }
141 
ToFrontendConfig(const MiiConfig & config) const142 Frontend::MiiSelectorConfig MiiSelector::ToFrontendConfig(const MiiConfig& config) const {
143     Frontend::MiiSelectorConfig frontend_config;
144     frontend_config.enable_cancel_button = config.enable_cancel_button == 1;
145     frontend_config.title = Common::UTF16BufferToUTF8(config.title);
146     frontend_config.initially_selected_mii_index = config.initially_selected_mii_index;
147     return frontend_config;
148 }
149 } // namespace HLE::Applets
150