summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/am/applets/applets.h
blob: adc973dad0f9716975a219ff13a30484db8a50e5 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <memory>
#include <queue>
#include "common/swap.h"
#include "core/hle/kernel/object.h"
#include "core/hle/kernel/writable_event.h"

union ResultCode;

namespace Core::Frontend {
class ECommerceApplet;
class ErrorApplet;
class ParentalControlsApplet;
class PhotoViewerApplet;
class ProfileSelectApplet;
class SoftwareKeyboardApplet;
class WebBrowserApplet;
} // namespace Core::Frontend

namespace Service::AM {

class IStorage;

namespace Applets {

enum class AppletId : u32 {
    OverlayDisplay = 0x02,
    QLaunch = 0x03,
    Starter = 0x04,
    Auth = 0x0A,
    Cabinet = 0x0B,
    Controller = 0x0C,
    DataErase = 0x0D,
    Error = 0x0E,
    NetConnect = 0x0F,
    ProfileSelect = 0x10,
    SoftwareKeyboard = 0x11,
    MiiEdit = 0x12,
    LibAppletWeb = 0x13,
    LibAppletShop = 0x14,
    PhotoViewer = 0x15,
    Settings = 0x16,
    LibAppletOff = 0x17,
    LibAppletWhitelisted = 0x18,
    LibAppletAuth = 0x19,
    MyPage = 0x1A,
};

class AppletDataBroker final {
public:
    AppletDataBroker();
    ~AppletDataBroker();

    struct RawChannelData {
        std::vector<std::vector<u8>> normal;
        std::vector<std::vector<u8>> interactive;
    };

    // Retrieves but does not pop the data sent to applet.
    RawChannelData PeekDataToAppletForDebug() const;

    std::unique_ptr<IStorage> PopNormalDataToGame();
    std::unique_ptr<IStorage> PopNormalDataToApplet();

    std::unique_ptr<IStorage> PopInteractiveDataToGame();
    std::unique_ptr<IStorage> PopInteractiveDataToApplet();

    void PushNormalDataFromGame(IStorage storage);
    void PushNormalDataFromApplet(IStorage storage);

    void PushInteractiveDataFromGame(IStorage storage);
    void PushInteractiveDataFromApplet(IStorage storage);

    void SignalStateChanged() const;

    Kernel::SharedPtr<Kernel::ReadableEvent> GetNormalDataEvent() const;
    Kernel::SharedPtr<Kernel::ReadableEvent> GetInteractiveDataEvent() const;
    Kernel::SharedPtr<Kernel::ReadableEvent> GetStateChangedEvent() const;

private:
    // Queues are named from applet's perspective

    // PopNormalDataToApplet and PushNormalDataFromGame
    std::deque<std::unique_ptr<IStorage>> in_channel;

    // PopNormalDataToGame and PushNormalDataFromApplet
    std::deque<std::unique_ptr<IStorage>> out_channel;

    // PopInteractiveDataToApplet and PushInteractiveDataFromGame
    std::deque<std::unique_ptr<IStorage>> in_interactive_channel;

    // PopInteractiveDataToGame and PushInteractiveDataFromApplet
    std::deque<std::unique_ptr<IStorage>> out_interactive_channel;

    Kernel::EventPair state_changed_event;

    // Signaled on PushNormalDataFromApplet
    Kernel::EventPair pop_out_data_event;

    // Signaled on PushInteractiveDataFromApplet
    Kernel::EventPair pop_interactive_out_data_event;
};

class Applet {
public:
    Applet();
    virtual ~Applet();

    virtual void Initialize();

    virtual bool TransactionComplete() const = 0;
    virtual ResultCode GetStatus() const = 0;
    virtual void ExecuteInteractive() = 0;
    virtual void Execute() = 0;

    bool IsInitialized() const {
        return initialized;
    }

    AppletDataBroker& GetBroker() {
        return broker;
    }

    const AppletDataBroker& GetBroker() const {
        return broker;
    }

protected:
    struct CommonArguments {
        u32_le arguments_version;
        u32_le size;
        u32_le library_version;
        u32_le theme_color;
        u8 play_startup_sound;
        u64_le system_tick;
    };
    static_assert(sizeof(CommonArguments) == 0x20, "CommonArguments has incorrect size.");

    CommonArguments common_args{};
    AppletDataBroker broker;
    bool initialized = false;
};

struct AppletFrontendSet {
    using ParentalControlsApplet = std::unique_ptr<Core::Frontend::ParentalControlsApplet>;
    using ErrorApplet = std::unique_ptr<Core::Frontend::ErrorApplet>;
    using PhotoViewer = std::unique_ptr<Core::Frontend::PhotoViewerApplet>;
    using ProfileSelect = std::unique_ptr<Core::Frontend::ProfileSelectApplet>;
    using SoftwareKeyboard = std::unique_ptr<Core::Frontend::SoftwareKeyboardApplet>;
    using WebBrowser = std::unique_ptr<Core::Frontend::WebBrowserApplet>;
    using ECommerceApplet = std::unique_ptr<Core::Frontend::ECommerceApplet>;

    AppletFrontendSet();
    AppletFrontendSet(ParentalControlsApplet parental_controls, ErrorApplet error,
                      PhotoViewer photo_viewer, ProfileSelect profile_select,
                      SoftwareKeyboard software_keyboard, WebBrowser web_browser,
                      ECommerceApplet e_commerce);
    ~AppletFrontendSet();

    AppletFrontendSet(const AppletFrontendSet&) = delete;
    AppletFrontendSet& operator=(const AppletFrontendSet&) = delete;

    AppletFrontendSet(AppletFrontendSet&&) noexcept;
    AppletFrontendSet& operator=(AppletFrontendSet&&) noexcept;

    ParentalControlsApplet parental_controls;
    ErrorApplet error;
    PhotoViewer photo_viewer;
    ProfileSelect profile_select;
    SoftwareKeyboard software_keyboard;
    WebBrowser web_browser;
    ECommerceApplet e_commerce;
};

class AppletManager {
public:
    AppletManager();
    ~AppletManager();

    void SetAppletFrontendSet(AppletFrontendSet set);
    void SetDefaultAppletFrontendSet();
    void SetDefaultAppletsIfMissing();
    void ClearAll();

    std::shared_ptr<Applet> GetApplet(AppletId id, u64 current_process_title_id) const;

private:
    AppletFrontendSet frontend;
};

} // namespace Applets
} // namespace Service::AM