dplayx: Introduce impl_from_IDirectPlayLobby3A().
[wine] / dlls / dmsynth / synthsink.c
1 /*
2  * IDirectMusicSynthSink Implementation
3  *
4  * Copyright (C) 2003-2004 Rok Mandeljc
5  * Copyright (C) 2012 Christian Costa
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24 #include "dmsynth_private.h"
25 #include "initguid.h"
26 #include "uuids.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(dmsynth);
29
30 static inline IDirectMusicSynthSinkImpl *impl_from_IDirectMusicSynthSink(IDirectMusicSynthSink *iface)
31 {
32     return CONTAINING_RECORD(iface, IDirectMusicSynthSinkImpl, IDirectMusicSynthSink_iface);
33 }
34
35 /* IDirectMusicSynthSinkImpl IUnknown part: */
36 static HRESULT WINAPI IDirectMusicSynthSinkImpl_QueryInterface(LPDIRECTMUSICSYNTHSINK iface, REFIID riid, LPVOID *ret_iface)
37 {
38     IDirectMusicSynthSinkImpl *This = impl_from_IDirectMusicSynthSink(iface);
39
40     TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
41
42     if (IsEqualIID (riid, &IID_IUnknown) ||
43         IsEqualIID (riid, &IID_IDirectMusicSynthSink))
44     {
45         IUnknown_AddRef(iface);
46         *ret_iface = iface;
47         return S_OK;
48     }
49     else if (IsEqualIID(riid, &IID_IKsControl))
50     {
51         IUnknown_AddRef(iface);
52         *ret_iface = &This->IKsControl_iface;
53         return S_OK;
54     }
55
56     *ret_iface = NULL;
57
58     WARN("(%p)->(%s, %p): not found\n", iface, debugstr_dmguid(riid), ret_iface);
59
60     return E_NOINTERFACE;
61 }
62
63 static ULONG WINAPI IDirectMusicSynthSinkImpl_AddRef(LPDIRECTMUSICSYNTHSINK iface)
64 {
65     IDirectMusicSynthSinkImpl *This = impl_from_IDirectMusicSynthSink(iface);
66     ULONG ref = InterlockedIncrement(&This->ref);
67
68     TRACE("(%p)->(): new ref = %u\n", This, ref);
69
70     DMSYNTH_LockModule();
71
72     return ref;
73 }
74
75 static ULONG WINAPI IDirectMusicSynthSinkImpl_Release(LPDIRECTMUSICSYNTHSINK iface)
76 {
77     IDirectMusicSynthSinkImpl *This = impl_from_IDirectMusicSynthSink(iface);
78     ULONG ref = InterlockedDecrement(&This->ref);
79
80     TRACE("(%p)->(): new ref = %u\n", This, ref);
81
82     if (!ref) {
83         if (This->latency_clock)
84             IReferenceClock_Release(This->latency_clock);
85         HeapFree(GetProcessHeap(), 0, This);
86     }
87
88     DMSYNTH_UnlockModule();
89
90     return ref;
91 }
92
93 /* IDirectMusicSynthSinkImpl IDirectMusicSynthSink part: */
94 static HRESULT WINAPI IDirectMusicSynthSinkImpl_Init(LPDIRECTMUSICSYNTHSINK iface, IDirectMusicSynth* synth)
95 {
96     IDirectMusicSynthSinkImpl *This = impl_from_IDirectMusicSynthSink(iface);
97
98     FIXME("(%p)->(%p): stub\n", This, synth);
99
100     return S_OK;
101 }
102
103 static HRESULT WINAPI IDirectMusicSynthSinkImpl_SetMasterClock(LPDIRECTMUSICSYNTHSINK iface, IReferenceClock* clock)
104 {
105     IDirectMusicSynthSinkImpl *This = impl_from_IDirectMusicSynthSink(iface);
106
107     FIXME("(%p)->(%p): stub\n", This, clock);
108
109     return S_OK;
110 }
111
112 static HRESULT WINAPI IDirectMusicSynthSinkImpl_GetLatencyClock(LPDIRECTMUSICSYNTHSINK iface, IReferenceClock** clock)
113 {
114     IDirectMusicSynthSinkImpl *This = impl_from_IDirectMusicSynthSink(iface);
115
116     TRACE("(%p)->(%p)\n", iface, clock);
117
118     if (!clock)
119         return E_POINTER;
120
121     *clock = This->latency_clock;
122     IReferenceClock_AddRef(This->latency_clock);
123
124     return S_OK;
125 }
126
127 static HRESULT WINAPI IDirectMusicSynthSinkImpl_Activate(LPDIRECTMUSICSYNTHSINK iface, BOOL enable)
128 {
129     IDirectMusicSynthSinkImpl *This = impl_from_IDirectMusicSynthSink(iface);
130
131     FIXME("(%p)->(%d): stub\n", This, enable);
132
133     return S_OK;
134 }
135
136 static HRESULT WINAPI IDirectMusicSynthSinkImpl_SampleToRefTime(LPDIRECTMUSICSYNTHSINK iface, LONGLONG sample_time, REFERENCE_TIME* ref_time)
137 {
138     IDirectMusicSynthSinkImpl *This = impl_from_IDirectMusicSynthSink(iface);
139
140     FIXME("(%p)->(0x%s, %p): stub\n", This, wine_dbgstr_longlong(sample_time), ref_time);
141
142     return S_OK;
143 }
144
145 static HRESULT WINAPI IDirectMusicSynthSinkImpl_RefTimeToSample(LPDIRECTMUSICSYNTHSINK iface, REFERENCE_TIME ref_time, LONGLONG* sample_time)
146 {
147     IDirectMusicSynthSinkImpl *This = impl_from_IDirectMusicSynthSink(iface);
148
149     FIXME("(%p)->(0x%s, %p): stub\n", This, wine_dbgstr_longlong(ref_time), sample_time);
150
151     return S_OK;
152 }
153
154 static HRESULT WINAPI IDirectMusicSynthSinkImpl_SetDirectSound(LPDIRECTMUSICSYNTHSINK iface, LPDIRECTSOUND dsound, LPDIRECTSOUNDBUFFER dsound_buffer)
155 {
156     IDirectMusicSynthSinkImpl *This = impl_from_IDirectMusicSynthSink(iface);
157
158     FIXME("(%p)->(%p, %p): stub\n", This, dsound, dsound_buffer);
159
160     return S_OK;
161 }
162
163 static HRESULT WINAPI IDirectMusicSynthSinkImpl_GetDesiredBufferSize(LPDIRECTMUSICSYNTHSINK iface, LPDWORD buffer_size_in_samples)
164 {
165     IDirectMusicSynthSinkImpl *This = impl_from_IDirectMusicSynthSink(iface);
166
167     FIXME("(%p)->(%p): stub\n", This, buffer_size_in_samples);
168
169     return S_OK;
170 }
171
172 static const IDirectMusicSynthSinkVtbl DirectMusicSynthSink_Vtbl = {
173         IDirectMusicSynthSinkImpl_QueryInterface,
174         IDirectMusicSynthSinkImpl_AddRef,
175         IDirectMusicSynthSinkImpl_Release,
176         IDirectMusicSynthSinkImpl_Init,
177         IDirectMusicSynthSinkImpl_SetMasterClock,
178         IDirectMusicSynthSinkImpl_GetLatencyClock,
179         IDirectMusicSynthSinkImpl_Activate,
180         IDirectMusicSynthSinkImpl_SampleToRefTime,
181         IDirectMusicSynthSinkImpl_RefTimeToSample,
182         IDirectMusicSynthSinkImpl_SetDirectSound,
183         IDirectMusicSynthSinkImpl_GetDesiredBufferSize
184 };
185
186 static inline IDirectMusicSynthSinkImpl *impl_from_IKsControl(IKsControl *iface)
187 {
188     return CONTAINING_RECORD(iface, IDirectMusicSynthSinkImpl, IKsControl_iface);
189 }
190
191 static HRESULT WINAPI DMSynthSinkImpl_IKsControl_QueryInterface(IKsControl* iface, REFIID riid, LPVOID *ppobj)
192 {
193     IDirectMusicSynthSinkImpl *This = impl_from_IKsControl(iface);
194
195     return IDirectMusicSynthSinkImpl_QueryInterface(&This->IDirectMusicSynthSink_iface, riid, ppobj);
196 }
197
198 static ULONG WINAPI DMSynthSinkImpl_IKsControl_AddRef(IKsControl* iface)
199 {
200     IDirectMusicSynthSinkImpl *This = impl_from_IKsControl(iface);
201
202     return IDirectMusicSynthSinkImpl_AddRef(&This->IDirectMusicSynthSink_iface);
203 }
204
205 static ULONG WINAPI DMSynthSinkImpl_IKsControl_Release(IKsControl* iface)
206 {
207     IDirectMusicSynthSinkImpl *This = impl_from_IKsControl(iface);
208
209     return IDirectMusicSynthSinkImpl_Release(&This->IDirectMusicSynthSink_iface);
210 }
211
212 static HRESULT WINAPI DMSynthSinkImpl_IKsControl_KsProperty(IKsControl* iface, PKSPROPERTY Property, ULONG PropertyLength, LPVOID PropertyData,
213                                                             ULONG DataLength, ULONG* BytesReturned)
214 {
215     TRACE("(%p)->(%p, %u, %p, %u, %p)\n", iface, Property, PropertyLength, PropertyData, DataLength, BytesReturned);
216
217     TRACE("Property = %s - %u - %u\n", debugstr_guid(&Property->u.s.Set), Property->u.s.Id, Property->u.s.Flags);
218
219     if (Property->u.s.Flags != KSPROPERTY_TYPE_GET)
220     {
221         FIXME("Property flags %u not yet supported\n", Property->u.s.Flags);
222         return S_FALSE;
223     }
224
225     if (DataLength <  sizeof(DWORD))
226         return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
227
228     if (IsEqualGUID(&Property->u.s.Set, &GUID_DMUS_PROP_SinkUsesDSound))
229     {
230         *(DWORD*)PropertyData = TRUE;
231         *BytesReturned = sizeof(DWORD);
232     }
233     else
234     {
235         FIXME("Unknown property %s\n", debugstr_guid(&Property->u.s.Set));
236         *(DWORD*)PropertyData = FALSE;
237         *BytesReturned = sizeof(DWORD);
238     }
239
240     return S_OK;
241 }
242
243 static HRESULT WINAPI DMSynthSinkImpl_IKsControl_KsMethod(IKsControl* iface, PKSMETHOD Method, ULONG MethodLength, LPVOID MethodData,
244                                                           ULONG DataLength, ULONG* BytesReturned)
245 {
246     FIXME("(%p)->(%p, %u, %p, %u, %p): stub\n", iface, Method, MethodLength, MethodData, DataLength, BytesReturned);
247
248     return E_NOTIMPL;
249 }
250
251 static HRESULT WINAPI DMSynthSinkImpl_IKsControl_KsEvent(IKsControl* iface, PKSEVENT Event, ULONG EventLength, LPVOID EventData,
252                                                          ULONG DataLength, ULONG* BytesReturned)
253 {
254     FIXME("(%p)->(%p, %u, %p, %u, %p): stub\n", iface, Event, EventLength, EventData, DataLength, BytesReturned);
255
256     return E_NOTIMPL;
257 }
258
259
260 static const IKsControlVtbl DMSynthSinkImpl_IKsControl_Vtbl = {
261     DMSynthSinkImpl_IKsControl_QueryInterface,
262     DMSynthSinkImpl_IKsControl_AddRef,
263     DMSynthSinkImpl_IKsControl_Release,
264     DMSynthSinkImpl_IKsControl_KsProperty,
265     DMSynthSinkImpl_IKsControl_KsMethod,
266     DMSynthSinkImpl_IKsControl_KsEvent
267 };
268
269 /* for ClassFactory */
270 HRESULT WINAPI DMUSIC_CreateDirectMusicSynthSinkImpl(LPCGUID riid, LPVOID* ret_iface, LPUNKNOWN unkouter)
271 {
272     IDirectMusicSynthSinkImpl *obj;
273     HRESULT hr;
274
275     TRACE("(%p,%p,%p)\n", riid, ret_iface, unkouter);
276
277     *ret_iface = NULL;
278
279     obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicSynthSinkImpl));
280     if (!obj)
281         return E_OUTOFMEMORY;
282
283     obj->IDirectMusicSynthSink_iface.lpVtbl = &DirectMusicSynthSink_Vtbl;
284     obj->IKsControl_iface.lpVtbl = &DMSynthSinkImpl_IKsControl_Vtbl;
285     obj->ref = 0;
286
287     hr = CoCreateInstance(&CLSID_SystemClock, NULL, CLSCTX_INPROC_SERVER, &IID_IReferenceClock, (LPVOID*)&obj->latency_clock);
288     if (FAILED(hr))
289     {
290         HeapFree(GetProcessHeap(), 0, obj);
291         return hr;
292     }
293
294     hr = IDirectMusicSynthSinkImpl_QueryInterface((LPDIRECTMUSICSYNTHSINK)obj, riid, ret_iface);
295     if (FAILED(hr))
296     {
297         IReferenceClock_Release(obj->latency_clock);
298         HeapFree(GetProcessHeap(), 0, obj);
299         return hr;
300     }
301
302     return S_OK;
303 }