summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/am/applets/profile_select.cpp
blob: 77fba16c7d1d6ea504b584a7321b931a16eb6e7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <cstring>

#include "common/assert.h"
#include "common/string_util.h"
#include "core/core.h"
#include "core/frontend/applets/profile_select.h"
#include "core/hle/service/am/am.h"
#include "core/hle/service/am/applets/profile_select.h"

namespace Service::AM::Applets {

constexpr ResultCode ERR_USER_CANCELLED_SELECTION{ErrorModule::Account, 1};

ProfileSelect::ProfileSelect(Core::System& system_,
                             const Core::Frontend::ProfileSelectApplet& frontend_)
    : Applet{system_.Kernel()}, frontend{frontend_}, system{system_} {}

ProfileSelect::~ProfileSelect() = default;

void ProfileSelect::Initialize() {
    complete = false;
    status = RESULT_SUCCESS;
    final_data.clear();

    Applet::Initialize();

    const auto user_config_storage = broker.PopNormalDataToApplet();
    ASSERT(user_config_storage != nullptr);
    const auto& user_config = user_config_storage->GetData();

    ASSERT(user_config.size() >= sizeof(UserSelectionConfig));
    std::memcpy(&config, user_config.data(), sizeof(UserSelectionConfig));
}

bool ProfileSelect::TransactionComplete() const {
    return complete;
}

ResultCode ProfileSelect::GetStatus() const {
    return status;
}

void ProfileSelect::ExecuteInteractive() {
    UNREACHABLE_MSG("Attempted to call interactive execution on non-interactive applet.");
}

void ProfileSelect::Execute() {
    if (complete) {
        broker.PushNormalDataFromApplet(std::make_shared<IStorage>(system, std::move(final_data)));
        return;
    }

    frontend.SelectProfile([this](std::optional<Common::UUID> uuid) { SelectionComplete(uuid); });
}

void ProfileSelect::SelectionComplete(std::optional<Common::UUID> uuid) {
    UserSelectionOutput output{};

    if (uuid.has_value() && uuid->uuid != Common::INVALID_UUID) {
        output.result = 0;
        output.uuid_selected = uuid->uuid;
    } else {
        status = ERR_USER_CANCELLED_SELECTION;
        output.result = ERR_USER_CANCELLED_SELECTION.raw;
        output.uuid_selected = Common::INVALID_UUID;
    }

    final_data = std::vector<u8>(sizeof(UserSelectionOutput));
    std::memcpy(final_data.data(), &output, final_data.size());
    broker.PushNormalDataFromApplet(std::make_shared<IStorage>(system, std::move(final_data)));
    broker.SignalStateChanged();
}

} // namespace Service::AM::Applets