2 * IDirectMusic8 Implementation
4 * Copyright (C) 2003-2004 Rok Mandeljc
5 * Copyright (C) 2012 Christian Costa
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.
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.
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
24 #include "dmusic_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
28 static inline IDirectMusic8Impl *impl_from_IDirectMusic8(IDirectMusic8 *iface)
30 return CONTAINING_RECORD(iface, IDirectMusic8Impl, IDirectMusic8_iface);
33 /* IDirectMusic8Impl IUnknown part: */
34 static HRESULT WINAPI IDirectMusic8Impl_QueryInterface(LPDIRECTMUSIC8 iface, REFIID riid, LPVOID *ret_iface)
36 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
38 TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
40 if (IsEqualIID (riid, &IID_IUnknown) ||
41 IsEqualIID (riid, &IID_IDirectMusic) ||
42 IsEqualIID (riid, &IID_IDirectMusic2) ||
43 IsEqualIID (riid, &IID_IDirectMusic8))
45 IDirectMusic8_AddRef(iface);
52 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
57 static ULONG WINAPI IDirectMusic8Impl_AddRef(LPDIRECTMUSIC8 iface)
59 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
60 ULONG ref = InterlockedIncrement(&This->ref);
62 TRACE("(%p)->(): new ref = %u\n", This, ref);
69 static ULONG WINAPI IDirectMusic8Impl_Release(LPDIRECTMUSIC8 iface)
71 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
72 ULONG ref = InterlockedDecrement(&This->ref);
74 TRACE("(%p)->(): new ref = %u\n", This, ref);
77 HeapFree(GetProcessHeap(), 0, This->ppPorts);
78 HeapFree(GetProcessHeap(), 0, This);
81 DMUSIC_UnlockModule();
86 /* IDirectMusic8Impl IDirectMusic part: */
87 static HRESULT WINAPI IDirectMusic8Impl_EnumPort(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_PORTCAPS port_caps)
89 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
92 const WCHAR emulated[] = {' ','[','E','m','u','l','a','t','e','d',']',0};
94 TRACE("(%p, %d, %p)\n", This, index, port_caps);
99 /* NOTE: It seems some native versions get the rest of devices through dmusic32.EnumLegacyDevices...*sigh*...which is undocumented */
101 /* NOTE: Should we enum wave devices ? Native does not seem to */
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;
111 port_caps->guidPort = IID_IUnknown;
112 port_caps->guidPort.Data1 = index + 1;
114 nb_midi_out = midiOutGetNumDevs();
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));
128 if (index < (nb_midi_out + 1))
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));
140 nb_midi_in = midiInGetNumDevs();
142 if (index < (nb_midi_in + nb_midi_out + 1))
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));
154 if (index == (nb_midi_in + nb_midi_out + 1))
156 IDirectMusicSynth8* synth = NULL;
158 hr = CoCreateInstance(&CLSID_DirectMusicSynth, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicSynth8, (void**)&synth);
160 hr = IDirectMusicSynth8_GetPortCaps(synth, port_caps);
162 TRACE("Enumerating port: %s\n", debugstr_w(port_caps->wszDescription));
164 IDirectMusicSynth8_Release(synth);
171 static HRESULT WINAPI IDirectMusic8Impl_CreateMusicBuffer(LPDIRECTMUSIC8 iface, LPDMUS_BUFFERDESC buffer_desc, LPDIRECTMUSICBUFFER* buffer, LPUNKNOWN unkouter)
173 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
175 TRACE("(%p)->(%p, %p, %p)\n", This, buffer_desc, buffer, unkouter);
178 return CLASS_E_NOAGGREGATION;
180 if (!buffer_desc || !buffer)
183 return DMUSIC_CreateDirectMusicBufferImpl(buffer_desc, (LPVOID)buffer);
186 static HRESULT WINAPI IDirectMusic8Impl_CreatePort(LPDIRECTMUSIC8 iface, REFCLSID rclsidPort, LPDMUS_PORTPARAMS pPortParams, LPDIRECTMUSICPORT* ppPort, LPUNKNOWN pUnkOuter)
188 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
190 DMUS_PORTCAPS PortCaps;
191 IDirectMusicPort* pNewPort = NULL;
194 const GUID *request_port = rclsidPort;
196 TRACE("(%p, %s, %p, %p, %p)\n", This, debugstr_dmguid(rclsidPort), pPortParams, ppPort, pUnkOuter);
198 if (TRACE_ON(dmusic))
199 dump_DMUS_PORTPARAMS(pPortParams);
208 return CLASS_E_NOAGGREGATION;
210 if (TRACE_ON(dmusic))
211 dump_DMUS_PORTPARAMS(pPortParams);
213 ZeroMemory(&PortCaps, sizeof(DMUS_PORTCAPS));
214 PortCaps.dwSize = sizeof(DMUS_PORTCAPS);
216 if(IsEqualGUID(request_port, &GUID_NULL)){
217 hr = IDirectMusic8_GetDefaultPort(iface, &default_port);
220 request_port = &default_port;
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);
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;
238 return E_NOINTERFACE;
241 static HRESULT WINAPI IDirectMusic8Impl_EnumMasterClock(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_CLOCKINFO clock_info)
243 TRACE("(%p)->(%d, %p)\n", iface, index, clock_info);
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 };
256 clock_info->ctType = 0;
257 clock_info->guidClock = guid_system_clock;
258 strcpyW(clock_info->wszDescription, name_system_clock);
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 };
265 clock_info->ctType = 0;
266 clock_info->guidClock = guid_dsound_clock;
267 strcpyW(clock_info->wszDescription, name_dsound_clock);
273 static HRESULT WINAPI IDirectMusic8Impl_GetMasterClock(LPDIRECTMUSIC8 iface, LPGUID guid_clock, IReferenceClock** reference_clock)
275 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
277 TRACE("(%p)->(%p, %p)\n", This, guid_clock, reference_clock);
280 *guid_clock = This->pMasterClock->pClockInfo.guidClock;
282 *reference_clock = (IReferenceClock*)This->pMasterClock;
287 static HRESULT WINAPI IDirectMusic8Impl_SetMasterClock(LPDIRECTMUSIC8 iface, REFGUID rguidClock)
289 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
291 FIXME("(%p)->(%s): stub\n", This, debugstr_dmguid(rguidClock));
296 static HRESULT WINAPI IDirectMusic8Impl_Activate(LPDIRECTMUSIC8 iface, BOOL enable)
298 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
302 TRACE("(%p)->(%u)\n", This, enable);
304 for (i = 0; i < This->nrofports; i++)
306 hr = IDirectMusicPort_Activate(This->ppPorts[i], enable);
314 static HRESULT WINAPI IDirectMusic8Impl_GetDefaultPort(LPDIRECTMUSIC8 iface, LPGUID pguidPort)
316 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
318 DWORD returnTypeGUID, sizeOfReturnBuffer = 50;
319 char returnBuffer[51];
320 GUID defaultPortGUID;
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))
327 WARN(": registry entry missing\n" );
328 *pguidPort = CLSID_DirectMusicSynth;
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;
339 static HRESULT WINAPI IDirectMusic8Impl_SetDirectSound(LPDIRECTMUSIC8 iface, LPDIRECTSOUND dsound, HWND wnd)
341 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
343 FIXME("(%p)->(%p, %p): stub\n", This, dsound, wnd);
348 static HRESULT WINAPI IDirectMusic8Impl_SetExternalMasterClock(LPDIRECTMUSIC8 iface, IReferenceClock* clock)
350 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
352 FIXME("(%p)->(%p): stub\n", This, clock);
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
373 /* For ClassFactory */
374 HRESULT WINAPI DMUSIC_CreateDirectMusicImpl(LPCGUID riid, LPVOID* ret_iface, LPUNKNOWN unkouter)
376 IDirectMusic8Impl *dmusic;
379 TRACE("(%p,%p,%p)\n", riid, ret_iface, unkouter);
383 dmusic = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusic8Impl));
385 return E_OUTOFMEMORY;
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);
394 HeapFree(GetProcessHeap(), 0, dmusic);
398 ret = IDirectMusic8Impl_QueryInterface(&dmusic->IDirectMusic8_iface, riid, ret_iface);
400 IReferenceClock_Release(&dmusic->pMasterClock->IReferenceClock_iface);
401 HeapFree(GetProcessHeap(), 0, dmusic);