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