dmusic: Rename params in IDirectMusic8Impl_GetDefaultPort.
[wine] / dlls / dmusic / dmusic.c
1 /*
2  * IDirectMusic8 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 #include <stdio.h>
23
24 #include "dmusic_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
27
28 static inline IDirectMusic8Impl *impl_from_IDirectMusic8(IDirectMusic8 *iface)
29 {
30     return CONTAINING_RECORD(iface, IDirectMusic8Impl, IDirectMusic8_iface);
31 }
32
33 /* IDirectMusic8Impl IUnknown part: */
34 static HRESULT WINAPI IDirectMusic8Impl_QueryInterface(LPDIRECTMUSIC8 iface, REFIID riid, LPVOID *ret_iface)
35 {
36     IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
37
38     TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
39
40     if (IsEqualIID (riid, &IID_IUnknown) ||
41         IsEqualIID (riid, &IID_IDirectMusic) ||
42         IsEqualIID (riid, &IID_IDirectMusic2) ||
43         IsEqualIID (riid, &IID_IDirectMusic8))
44     {
45         IDirectMusic8_AddRef(iface);
46         *ret_iface = iface;
47         return S_OK;
48     }
49
50     *ret_iface = NULL;
51
52     WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
53
54     return E_NOINTERFACE;
55 }
56
57 static ULONG WINAPI IDirectMusic8Impl_AddRef(LPDIRECTMUSIC8 iface)
58 {
59     IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
60     ULONG ref = InterlockedIncrement(&This->ref);
61
62     TRACE("(%p)->(): new ref = %u\n", This, ref);
63
64     DMUSIC_LockModule();
65
66     return ref;
67 }
68
69 static ULONG WINAPI IDirectMusic8Impl_Release(LPDIRECTMUSIC8 iface)
70 {
71     IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
72     ULONG ref = InterlockedDecrement(&This->ref);
73
74     TRACE("(%p)->(): new ref = %u\n", This, ref);
75
76     if (!ref) {
77         HeapFree(GetProcessHeap(), 0, This->system_ports);
78         HeapFree(GetProcessHeap(), 0, This->ppPorts);
79         HeapFree(GetProcessHeap(), 0, This);
80     }
81
82     DMUSIC_UnlockModule();
83
84     return ref;
85 }
86
87 /* IDirectMusic8Impl IDirectMusic part: */
88 static HRESULT WINAPI IDirectMusic8Impl_EnumPort(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_PORTCAPS port_caps)
89 {
90     IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
91
92     TRACE("(%p, %d, %p)\n", This, index, port_caps);
93
94     if (!port_caps)
95         return E_POINTER;
96
97     if (index >= This->nb_system_ports)
98         return S_FALSE;
99
100     *port_caps = This->system_ports[index].caps;
101
102     return S_OK;
103 }
104
105 static HRESULT WINAPI IDirectMusic8Impl_CreateMusicBuffer(LPDIRECTMUSIC8 iface, LPDMUS_BUFFERDESC buffer_desc, LPDIRECTMUSICBUFFER* buffer, LPUNKNOWN unkouter)
106 {
107     IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
108
109     TRACE("(%p)->(%p, %p, %p)\n", This, buffer_desc, buffer, unkouter);
110
111     if (unkouter)
112         return CLASS_E_NOAGGREGATION;
113
114     if (!buffer_desc || !buffer)
115         return E_POINTER;
116
117     return DMUSIC_CreateDirectMusicBufferImpl(buffer_desc, (LPVOID)buffer);
118 }
119
120 static HRESULT WINAPI IDirectMusic8Impl_CreatePort(LPDIRECTMUSIC8 iface, REFCLSID rclsid_port, LPDMUS_PORTPARAMS port_params, LPDIRECTMUSICPORT* port, LPUNKNOWN unkouter)
121 {
122     IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
123     int i;
124     DMUS_PORTCAPS port_caps;
125     IDirectMusicPort* new_port = NULL;
126     HRESULT hr;
127     GUID default_port;
128     const GUID *request_port = rclsid_port;
129
130     TRACE("(%p)->(%s, %p, %p, %p)\n", This, debugstr_dmguid(rclsid_port), port_params, port, unkouter);
131
132     if (TRACE_ON(dmusic))
133         dump_DMUS_PORTPARAMS(port_params);
134
135     if (!rclsid_port)
136         return E_POINTER;
137     if (!port_params)
138         return E_INVALIDARG;
139     if (!port)
140         return E_POINTER;
141     if (unkouter)
142         return CLASS_E_NOAGGREGATION;
143
144     if (TRACE_ON(dmusic))
145         dump_DMUS_PORTPARAMS(port_params);
146
147     ZeroMemory(&port_caps, sizeof(DMUS_PORTCAPS));
148     port_caps.dwSize = sizeof(DMUS_PORTCAPS);
149
150     if (IsEqualGUID(request_port, &GUID_NULL)) {
151         hr = IDirectMusic8_GetDefaultPort(iface, &default_port);
152         if(FAILED(hr))
153             return hr;
154         request_port = &default_port;
155     }
156
157     for (i = 0; S_FALSE != IDirectMusic8Impl_EnumPort(iface, i, &port_caps); i++) {
158         if (IsEqualCLSID(request_port, &port_caps.guidPort)) {
159             hr = This->system_ports[i].create(&IID_IDirectMusicPort, (LPVOID*)&new_port, (LPUNKNOWN)This, port_params, &port_caps, This->system_ports[i].device);
160             if (FAILED(hr)) {
161                  *port = NULL;
162                  return hr;
163             }
164             This->nrofports++;
165             if (!This->ppPorts)
166                 This->ppPorts = HeapAlloc(GetProcessHeap(), 0, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
167             else
168                 This->ppPorts = HeapReAlloc(GetProcessHeap(), 0, This->ppPorts, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
169             This->ppPorts[This->nrofports - 1] = new_port;
170             *port = new_port;
171             return S_OK;
172         }
173     }
174
175     return E_NOINTERFACE;
176 }
177
178 static HRESULT WINAPI IDirectMusic8Impl_EnumMasterClock(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_CLOCKINFO clock_info)
179 {
180     TRACE("(%p)->(%d, %p)\n", iface, index, clock_info);
181
182     if (!clock_info)
183         return E_POINTER;
184
185     if (index > 1)
186         return S_FALSE;
187
188     if (!index)
189     {
190         static const GUID guid_system_clock = { 0x58d58419, 0x71b4, 0x11d1, { 0xa7, 0x4c, 0x00, 0x00, 0xf8, 0x75, 0xac, 0x12 } };
191         static const WCHAR name_system_clock[] = { 'S','y','s','t','e','m',' ','C','l','o','c','k',0 };
192
193         clock_info->ctType = 0;
194         clock_info->guidClock = guid_system_clock;
195         strcpyW(clock_info->wszDescription, name_system_clock);
196     }
197     else
198     {
199         static const GUID guid_dsound_clock = { 0x58d58420, 0x71b4, 0x11d1, { 0xa7, 0x4c, 0x00, 0x00, 0xf8, 0x75, 0xac, 0x12 } };
200         static const WCHAR name_dsound_clock[] = { 'D','i','r','e','c','t','S','o','u','n','d',' ','C','l','o','c','k',0 };
201
202         clock_info->ctType = 0;
203         clock_info->guidClock = guid_dsound_clock;
204         strcpyW(clock_info->wszDescription, name_dsound_clock);
205     }
206
207     return S_OK;
208 }
209
210 static HRESULT WINAPI IDirectMusic8Impl_GetMasterClock(LPDIRECTMUSIC8 iface, LPGUID guid_clock, IReferenceClock** reference_clock)
211 {
212     IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
213
214     TRACE("(%p)->(%p, %p)\n", This, guid_clock, reference_clock);
215
216     if (guid_clock)
217         *guid_clock = This->pMasterClock->pClockInfo.guidClock;
218     if (reference_clock)
219         *reference_clock = (IReferenceClock*)This->pMasterClock;
220
221     return S_OK;
222 }
223
224 static HRESULT WINAPI IDirectMusic8Impl_SetMasterClock(LPDIRECTMUSIC8 iface, REFGUID rguidClock)
225 {
226     IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
227
228     FIXME("(%p)->(%s): stub\n", This, debugstr_dmguid(rguidClock));
229
230     return S_OK;
231 }
232
233 static HRESULT WINAPI IDirectMusic8Impl_Activate(LPDIRECTMUSIC8 iface, BOOL enable)
234 {
235     IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
236     int i;
237     HRESULT hr;
238
239     TRACE("(%p)->(%u)\n", This, enable);
240
241     for (i = 0; i < This->nrofports; i++)
242     {
243         hr = IDirectMusicPort_Activate(This->ppPorts[i], enable);
244         if (FAILED(hr))
245             return hr;
246     }
247
248     return S_OK;
249 }
250
251 static HRESULT WINAPI IDirectMusic8Impl_GetDefaultPort(LPDIRECTMUSIC8 iface, LPGUID guid_port)
252 {
253     IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
254     HKEY hkGUID;
255     DWORD returnTypeGUID, sizeOfReturnBuffer = 50;
256     char returnBuffer[51];
257     GUID defaultPortGUID;
258     WCHAR buff[51];
259
260     TRACE("(%p)->(%p)\n", This, guid_port);
261
262     if ((RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic\\Defaults" , 0, KEY_READ, &hkGUID) != ERROR_SUCCESS) ||
263         (RegQueryValueExA(hkGUID, "DefaultOutputPort", NULL, &returnTypeGUID, (LPBYTE)returnBuffer, &sizeOfReturnBuffer) != ERROR_SUCCESS))
264     {
265         WARN(": registry entry missing\n" );
266         *guid_port = CLSID_DirectMusicSynth;
267         return S_OK;
268     }
269     /* FIXME: Check return types to ensure we're interpreting data right */
270     MultiByteToWideChar(CP_ACP, 0, returnBuffer, -1, buff, sizeof(buff) / sizeof(WCHAR));
271     CLSIDFromString(buff, &defaultPortGUID);
272     *guid_port = defaultPortGUID;
273
274     return S_OK;
275 }
276
277 static HRESULT WINAPI IDirectMusic8Impl_SetDirectSound(LPDIRECTMUSIC8 iface, LPDIRECTSOUND dsound, HWND wnd)
278 {
279     IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
280
281     FIXME("(%p)->(%p, %p): stub\n", This, dsound, wnd);
282
283     return S_OK;
284 }
285
286 static HRESULT WINAPI IDirectMusic8Impl_SetExternalMasterClock(LPDIRECTMUSIC8 iface, IReferenceClock* clock)
287 {
288     IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
289
290     FIXME("(%p)->(%p): stub\n", This, clock);
291
292     return S_OK;
293 }
294
295 static const IDirectMusic8Vtbl DirectMusic8_Vtbl = {
296     IDirectMusic8Impl_QueryInterface,
297     IDirectMusic8Impl_AddRef,
298     IDirectMusic8Impl_Release,
299     IDirectMusic8Impl_EnumPort,
300     IDirectMusic8Impl_CreateMusicBuffer,
301     IDirectMusic8Impl_CreatePort,
302     IDirectMusic8Impl_EnumMasterClock,
303     IDirectMusic8Impl_GetMasterClock,
304     IDirectMusic8Impl_SetMasterClock,
305     IDirectMusic8Impl_Activate,
306     IDirectMusic8Impl_GetDefaultPort,
307     IDirectMusic8Impl_SetDirectSound,
308     IDirectMusic8Impl_SetExternalMasterClock
309 };
310
311 static void create_system_ports_list(IDirectMusic8Impl* object)
312 {
313     port_info * port;
314     const WCHAR emulated[] = {' ','[','E','m','u','l','a','t','e','d',']',0};
315     ULONG nb_ports;
316     ULONG nb_midi_out;
317     ULONG nb_midi_in;
318     MIDIOUTCAPSW caps_out;
319     MIDIINCAPSW caps_in;
320     IDirectMusicSynth8* synth;
321     HRESULT hr;
322     int i;
323
324     TRACE("(%p)\n", object);
325
326     /* NOTE:
327        - it seems some native versions get the rest of devices through dmusic32.EnumLegacyDevices...*sigh*...which is undocumented
328        - should we enum wave devices ? Native does not seem to
329     */
330
331     nb_midi_out = midiOutGetNumDevs();
332     nb_midi_in = midiInGetNumDevs();
333     nb_ports = 1 /* midi mapper */ + nb_midi_out + nb_midi_in + 1 /* synth port */;
334
335     port = object->system_ports = HeapAlloc(GetProcessHeap(), 0, nb_ports * sizeof(port_info));
336     if (!object->system_ports)
337         return;
338
339     /* Fill common port caps for all winmm ports */
340     for (i = 0; i < (nb_ports - 1 /* synth port*/); i++)
341     {
342         object->system_ports[i].caps.dwSize = sizeof(DMUS_PORTCAPS);
343         object->system_ports[i].caps.dwType = DMUS_PORT_WINMM_DRIVER;
344         object->system_ports[i].caps.dwMemorySize = 0;
345         object->system_ports[i].caps.dwMaxChannelGroups = 1;
346         object->system_ports[i].caps.dwMaxVoices = 0;
347         object->system_ports[i].caps.dwMaxAudioChannels = 0;
348         object->system_ports[i].caps.dwEffectFlags = DMUS_EFFECT_NONE;
349         /* Fake port GUID */
350         object->system_ports[i].caps.guidPort = IID_IUnknown;
351         object->system_ports[i].caps.guidPort.Data1 = i + 1;
352     }
353
354     /* Fill midi mapper port info */
355     port->device = MIDI_MAPPER;
356     port->create = DMUSIC_CreateMidiOutPortImpl;
357     midiOutGetDevCapsW(MIDI_MAPPER, &caps_out, sizeof(caps_out));
358     strcpyW(port->caps.wszDescription, caps_out.szPname);
359     strcatW(port->caps.wszDescription, emulated);
360     port->caps.dwFlags = DMUS_PC_SHAREABLE;
361     port->caps.dwClass = DMUS_PC_OUTPUTCLASS;
362     port++;
363
364     /* Fill midi out port info */
365     for (i = 0; i < nb_midi_out; i++)
366     {
367         port->device = i;
368         port->create = DMUSIC_CreateMidiOutPortImpl;
369         midiOutGetDevCapsW(i, &caps_out, sizeof(caps_out));
370         strcpyW(port->caps.wszDescription, caps_in.szPname);
371         strcatW(port->caps.wszDescription, emulated);
372         port->caps.dwFlags = DMUS_PC_SHAREABLE | DMUS_PC_EXTERNAL;
373         port->caps.dwClass = DMUS_PC_OUTPUTCLASS;
374         port++;
375     }
376
377     /* Fill midi in port info */
378     for (i = 0; i < nb_midi_in; i++)
379     {
380         port->device = i;
381         port->create = DMUSIC_CreateMidiInPortImpl;
382         midiInGetDevCapsW(i, &caps_in, sizeof(caps_in));
383         strcpyW(port->caps.wszDescription, caps_in.szPname);
384         strcatW(port->caps.wszDescription, emulated);
385         port->caps.dwFlags = DMUS_PC_EXTERNAL;
386         port->caps.dwClass = DMUS_PC_INPUTCLASS;
387         port++;
388     }
389
390     /* Fill synth port info */
391     port->create = DMUSIC_CreateSynthPortImpl;
392     hr = CoCreateInstance(&CLSID_DirectMusicSynth, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicSynth8, (void**)&synth);
393     if (SUCCEEDED(hr))
394     {
395         hr = IDirectMusicSynth8_GetPortCaps(synth, &port->caps);
396         IDirectMusicSynth8_Release(synth);
397     }
398     else
399     {
400         nb_ports--;
401     }
402
403     object->nb_system_ports = nb_ports;
404 }
405
406 /* For ClassFactory */
407 HRESULT WINAPI DMUSIC_CreateDirectMusicImpl(LPCGUID riid, LPVOID* ret_iface, LPUNKNOWN unkouter)
408 {
409     IDirectMusic8Impl *dmusic;
410     HRESULT ret;
411
412     TRACE("(%p,%p,%p)\n", riid, ret_iface, unkouter);
413
414     *ret_iface = NULL;
415
416     dmusic = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusic8Impl));
417     if (!dmusic)
418         return E_OUTOFMEMORY;
419
420     dmusic->IDirectMusic8_iface.lpVtbl = &DirectMusic8_Vtbl;
421     dmusic->ref = 0; /* Will be inited by QueryInterface */
422     dmusic->pMasterClock = NULL;
423     dmusic->ppPorts = NULL;
424     dmusic->nrofports = 0;
425     ret = DMUSIC_CreateReferenceClockImpl(&IID_IReferenceClock, (LPVOID*)&dmusic->pMasterClock, NULL);
426     if (FAILED(ret)) {
427         HeapFree(GetProcessHeap(), 0, dmusic);
428         return ret;
429     }
430
431     ret = IDirectMusic8Impl_QueryInterface(&dmusic->IDirectMusic8_iface, riid, ret_iface);
432     if (FAILED(ret)) {
433         IReferenceClock_Release(&dmusic->pMasterClock->IReferenceClock_iface);
434         HeapFree(GetProcessHeap(), 0, dmusic);
435         return ret;
436     }
437
438     create_system_ports_list(dmusic);
439
440     return S_OK;
441 }