aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Horizon/MmNv/Ipc/Request.cs
blob: c53ca18668cb1ec7e7f04bf164bf4a9909a95fd1 (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
using Ryujinx.Common.Logging;
using Ryujinx.Horizon.Common;
using Ryujinx.Horizon.Sdk.MmNv;
using Ryujinx.Horizon.Sdk.Sf;
using System.Collections.Generic;

namespace Ryujinx.Horizon.MmNv.Ipc
{
    partial class Request : IRequest
    {
        private readonly List<Session> _sessionList = new();

        private uint _uniqueId = 1;

        [CmifCommand(0)]
        public Result InitializeOld(Module module, uint fgmPriority, uint autoClearEvent)
        {
            bool isAutoClearEvent = autoClearEvent != 0;

            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { module, fgmPriority, isAutoClearEvent });

            Register(module, fgmPriority, isAutoClearEvent);

            return Result.Success;
        }

        [CmifCommand(1)]
        public Result FinalizeOld(Module module)
        {
            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { module });

            lock (_sessionList)
            {
                _sessionList.Remove(GetSessionByModule(module));
            }

            return Result.Success;
        }

        [CmifCommand(2)]
        public Result SetAndWaitOld(Module module, uint clockRateMin, int clockRateMax)
        {
            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { module, clockRateMin, clockRateMax });

            lock (_sessionList)
            {
                GetSessionByModule(module)?.SetAndWait(clockRateMin, clockRateMax);
            }

            return Result.Success;
        }

        [CmifCommand(3)]
        public Result GetOld(out uint clockRateActual, Module module)
        {
            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { module });

            lock (_sessionList)
            {
                Session session = GetSessionByModule(module);

                clockRateActual = session == null ? 0 : session.ClockRateMin;
            }

            return Result.Success;
        }

        [CmifCommand(4)]
        public Result Initialize(out uint requestId, Module module, uint fgmPriority, uint autoClearEvent)
        {
            bool isAutoClearEvent = autoClearEvent != 0;

            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { module, fgmPriority, isAutoClearEvent });

            requestId = Register(module, fgmPriority, isAutoClearEvent);

            return Result.Success;
        }

        [CmifCommand(5)]
        public Result Finalize(uint requestId)
        {
            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { requestId });

            lock (_sessionList)
            {
                _sessionList.Remove(GetSessionById(requestId));
            }

            return Result.Success;
        }

        [CmifCommand(6)]
        public Result SetAndWait(uint requestId, uint clockRateMin, int clockRateMax)
        {
            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { requestId, clockRateMin, clockRateMax });

            lock (_sessionList)
            {
                GetSessionById(requestId)?.SetAndWait(clockRateMin, clockRateMax);
            }

            return Result.Success;
        }

        [CmifCommand(7)]
        public Result Get(out uint clockRateActual, uint requestId)
        {
            Logger.Stub?.PrintStub(LogClass.ServiceMm, new { requestId });

            lock (_sessionList)
            {
                Session session = GetSessionById(requestId);

                clockRateActual = session == null ? 0 : session.ClockRateMin;
            }

            return Result.Success;
        }

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

            return null;
        }

        private Session GetSessionByModule(Module module)
        {
            foreach (Session session in _sessionList)
            {
                if (session.Module == module)
                {
                    return session;
                }
            }

            return null;
        }

        private uint Register(Module module, uint fgmPriority, bool isAutoClearEvent)
        {
            lock (_sessionList)
            {
                // Nintendo ignores the fgm priority as the other services were deprecated.
                Session session = new(_uniqueId++, module, isAutoClearEvent);

                _sessionList.Add(session);

                return session.Id;
            }
        }
    }
}