aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Services/Mm/IRequest.cs
blob: 31d325a942873d7756fef96b1d4b2546f31139c9 (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
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Services.Mm.Types;
using System.Collections.Generic;

namespace Ryujinx.HLE.HOS.Services.Mm
{
    [Service("mm:u")]
    class IRequest : IpcService
    {
        private readonly object _sessionListLock = new();
        private readonly List<MultiMediaSession> _sessionList = new();

        private uint _uniqueId = 1;

        public IRequest(ServiceCtx context) { }

        [CommandCmif(0)]
        // InitializeOld(u32, u32, u32)
        public ResultCode InitializeOld(ServiceCtx context)
        {
            MultiMediaOperationType operationType = (MultiMediaOperationType)context.RequestData.ReadUInt32();
            int fgmId = context.RequestData.ReadInt32();
            bool isAutoClearEvent = context.RequestData.ReadInt32() != 0;

            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { operationType, fgmId, isAutoClearEvent });

            Register(operationType, fgmId, isAutoClearEvent);

            return ResultCode.Success;
        }

        [CommandCmif(1)]
        // FinalizeOld(u32)
        public ResultCode FinalizeOld(ServiceCtx context)
        {
            MultiMediaOperationType operationType = (MultiMediaOperationType)context.RequestData.ReadUInt32();

            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { operationType });

            lock (_sessionListLock)
            {
                _sessionList.Remove(GetSessionByType(operationType));
            }

            return ResultCode.Success;
        }

        [CommandCmif(2)]
        // SetAndWaitOld(u32, u32, u32)
        public ResultCode SetAndWaitOld(ServiceCtx context)
        {
            MultiMediaOperationType operationType = (MultiMediaOperationType)context.RequestData.ReadUInt32();
            uint frequenceHz = context.RequestData.ReadUInt32();
            int timeout = context.RequestData.ReadInt32();

            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { operationType, frequenceHz, timeout });

            lock (_sessionListLock)
            {
                GetSessionByType(operationType)?.SetAndWait(frequenceHz, timeout);
            }

            return ResultCode.Success;
        }

        [CommandCmif(3)]
        // GetOld(u32) -> u32
        public ResultCode GetOld(ServiceCtx context)
        {
            MultiMediaOperationType operationType = (MultiMediaOperationType)context.RequestData.ReadUInt32();

            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { operationType });

            lock (_sessionListLock)
            {
                MultiMediaSession session = GetSessionByType(operationType);

                uint currentValue = session == null ? 0 : session.CurrentValue;

                context.ResponseData.Write(currentValue);
            }

            return ResultCode.Success;
        }

        [CommandCmif(4)]
        // Initialize(u32, u32, u32) -> u32
        public ResultCode Initialize(ServiceCtx context)
        {
            MultiMediaOperationType operationType = (MultiMediaOperationType)context.RequestData.ReadUInt32();
            int fgmId = context.RequestData.ReadInt32();
            bool isAutoClearEvent = context.RequestData.ReadInt32() != 0;

            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { operationType, fgmId, isAutoClearEvent });

            uint id = Register(operationType, fgmId, isAutoClearEvent);

            context.ResponseData.Write(id);

            return ResultCode.Success;
        }

        [CommandCmif(5)]
        // Finalize(u32)
        public ResultCode Finalize(ServiceCtx context)
        {
            uint id = context.RequestData.ReadUInt32();

            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { id });

            lock (_sessionListLock)
            {
                _sessionList.Remove(GetSessionById(id));
            }

            return ResultCode.Success;
        }

        [CommandCmif(6)]
        // SetAndWait(u32, u32, u32)
        public ResultCode SetAndWait(ServiceCtx context)
        {
            uint id = context.RequestData.ReadUInt32();
            uint frequenceHz = context.RequestData.ReadUInt32();
            int timeout = context.RequestData.ReadInt32();

            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { id, frequenceHz, timeout });

            lock (_sessionListLock)
            {
                GetSessionById(id)?.SetAndWait(frequenceHz, timeout);
            }

            return ResultCode.Success;
        }

        [CommandCmif(7)]
        // Get(u32) -> u32
        public ResultCode Get(ServiceCtx context)
        {
            uint id = context.RequestData.ReadUInt32();

            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { id });

            lock (_sessionListLock)
            {
                MultiMediaSession session = GetSessionById(id);

                uint currentValue = session == null ? 0 : session.CurrentValue;

                context.ResponseData.Write(currentValue);
            }

            return ResultCode.Success;
        }

        private MultiMediaSession GetSessionById(uint id)
        {
            foreach (MultiMediaSession session in _sessionList)
            {
                if (session.Id == id)
                {
                    return session;
                }
            }

            return null;
        }

        private MultiMediaSession GetSessionByType(MultiMediaOperationType type)
        {
            foreach (MultiMediaSession session in _sessionList)
            {
                if (session.Type == type)
                {
                    return session;
                }
            }

            return null;
        }

        private uint Register(MultiMediaOperationType type, int fgmId, bool isAutoClearEvent)
        {
            lock (_sessionListLock)
            {
                // Nintendo ignore the fgm id as the other interfaces were deprecated.
                MultiMediaSession session = new(_uniqueId++, type, isAutoClearEvent);

                _sessionList.Add(session);

                return session.Id;
            }
        }
    }
}