mshtml: Pass external connection to WebBrowser host, if possible.
[wine.git] / dlls / dmusic / dmusic.c
blob0c2584f3d687195847041a9be343ce504538d72b
1 /*
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
22 #include <stdio.h>
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);
46 *ret_iface = iface;
47 return S_OK;
50 *ret_iface = NULL;
52 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
54 return E_NOINTERFACE;
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);
64 DMUSIC_LockModule();
66 return 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);
76 if (!ref) {
77 HeapFree(GetProcessHeap(), 0, This->system_ports);
78 HeapFree(GetProcessHeap(), 0, This->ppPorts);
79 HeapFree(GetProcessHeap(), 0, This);
82 DMUSIC_UnlockModule();
84 return ref;
87 /* IDirectMusic8Impl IDirectMusic part: */
88 static HRESULT WINAPI IDirectMusic8Impl_EnumPort(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_PORTCAPS port_caps)
90 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
92 TRACE("(%p, %d, %p)\n", This, index, port_caps);
94 if (!port_caps)
95 return E_POINTER;
97 if (index >= This->nb_system_ports)
98 return S_FALSE;
100 *port_caps = This->system_ports[index].caps;
102 return S_OK;
105 static HRESULT WINAPI IDirectMusic8Impl_CreateMusicBuffer(LPDIRECTMUSIC8 iface, LPDMUS_BUFFERDESC buffer_desc, LPDIRECTMUSICBUFFER* buffer, LPUNKNOWN unkouter)
107 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
109 TRACE("(%p)->(%p, %p, %p)\n", This, buffer_desc, buffer, unkouter);
111 if (unkouter)
112 return CLASS_E_NOAGGREGATION;
114 if (!buffer_desc || !buffer)
115 return E_POINTER;
117 return DMUSIC_CreateDirectMusicBufferImpl(buffer_desc, (LPVOID)buffer);
120 static HRESULT WINAPI IDirectMusic8Impl_CreatePort(LPDIRECTMUSIC8 iface, REFCLSID rclsid_port, LPDMUS_PORTPARAMS port_params, LPDIRECTMUSICPORT* port, LPUNKNOWN unkouter)
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;
130 TRACE("(%p)->(%s, %p, %p, %p)\n", This, debugstr_dmguid(rclsid_port), port_params, port, unkouter);
132 if (!rclsid_port)
133 return E_POINTER;
134 if (!port_params)
135 return E_INVALIDARG;
136 if (!port)
137 return E_POINTER;
138 if (unkouter)
139 return CLASS_E_NOAGGREGATION;
141 if (TRACE_ON(dmusic))
142 dump_DMUS_PORTPARAMS(port_params);
144 ZeroMemory(&port_caps, sizeof(DMUS_PORTCAPS));
145 port_caps.dwSize = sizeof(DMUS_PORTCAPS);
147 if (IsEqualGUID(request_port, &GUID_NULL)) {
148 hr = IDirectMusic8_GetDefaultPort(iface, &default_port);
149 if(FAILED(hr))
150 return hr;
151 request_port = &default_port;
154 for (i = 0; S_FALSE != IDirectMusic8Impl_EnumPort(iface, i, &port_caps); i++) {
155 if (IsEqualCLSID(request_port, &port_caps.guidPort)) {
156 hr = This->system_ports[i].create(&IID_IDirectMusicPort, (LPVOID*)&new_port, (LPUNKNOWN)This, port_params, &port_caps, This->system_ports[i].device);
157 if (FAILED(hr)) {
158 *port = NULL;
159 return hr;
161 This->nrofports++;
162 if (!This->ppPorts)
163 This->ppPorts = HeapAlloc(GetProcessHeap(), 0, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
164 else
165 This->ppPorts = HeapReAlloc(GetProcessHeap(), 0, This->ppPorts, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
166 This->ppPorts[This->nrofports - 1] = new_port;
167 *port = new_port;
168 return S_OK;
172 return E_NOINTERFACE;
175 static HRESULT WINAPI IDirectMusic8Impl_EnumMasterClock(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_CLOCKINFO clock_info)
177 TRACE("(%p)->(%d, %p)\n", iface, index, clock_info);
179 if (!clock_info)
180 return E_POINTER;
182 if (index > 1)
183 return S_FALSE;
185 if (!index)
187 static const GUID guid_system_clock = { 0x58d58419, 0x71b4, 0x11d1, { 0xa7, 0x4c, 0x00, 0x00, 0xf8, 0x75, 0xac, 0x12 } };
188 static const WCHAR name_system_clock[] = { 'S','y','s','t','e','m',' ','C','l','o','c','k',0 };
190 clock_info->ctType = 0;
191 clock_info->guidClock = guid_system_clock;
192 strcpyW(clock_info->wszDescription, name_system_clock);
194 else
196 static const GUID guid_dsound_clock = { 0x58d58420, 0x71b4, 0x11d1, { 0xa7, 0x4c, 0x00, 0x00, 0xf8, 0x75, 0xac, 0x12 } };
197 static const WCHAR name_dsound_clock[] = { 'D','i','r','e','c','t','S','o','u','n','d',' ','C','l','o','c','k',0 };
199 clock_info->ctType = 0;
200 clock_info->guidClock = guid_dsound_clock;
201 strcpyW(clock_info->wszDescription, name_dsound_clock);
204 return S_OK;
207 static HRESULT WINAPI IDirectMusic8Impl_GetMasterClock(LPDIRECTMUSIC8 iface, LPGUID guid_clock, IReferenceClock** reference_clock)
209 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
211 TRACE("(%p)->(%p, %p)\n", This, guid_clock, reference_clock);
213 if (guid_clock)
214 *guid_clock = This->pMasterClock->pClockInfo.guidClock;
215 if (reference_clock)
216 *reference_clock = (IReferenceClock*)This->pMasterClock;
218 return S_OK;
221 static HRESULT WINAPI IDirectMusic8Impl_SetMasterClock(LPDIRECTMUSIC8 iface, REFGUID rguidClock)
223 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
225 FIXME("(%p)->(%s): stub\n", This, debugstr_dmguid(rguidClock));
227 return S_OK;
230 static HRESULT WINAPI IDirectMusic8Impl_Activate(LPDIRECTMUSIC8 iface, BOOL enable)
232 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
233 int i;
234 HRESULT hr;
236 TRACE("(%p)->(%u)\n", This, enable);
238 for (i = 0; i < This->nrofports; i++)
240 hr = IDirectMusicPort_Activate(This->ppPorts[i], enable);
241 if (FAILED(hr))
242 return hr;
245 return S_OK;
248 static HRESULT WINAPI IDirectMusic8Impl_GetDefaultPort(LPDIRECTMUSIC8 iface, LPGUID guid_port)
250 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
251 HKEY hkGUID;
252 DWORD returnTypeGUID, sizeOfReturnBuffer = 50;
253 char returnBuffer[51];
254 GUID defaultPortGUID;
255 WCHAR buff[51];
257 TRACE("(%p)->(%p)\n", This, guid_port);
259 if ((RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic\\Defaults" , 0, KEY_READ, &hkGUID) != ERROR_SUCCESS) ||
260 (RegQueryValueExA(hkGUID, "DefaultOutputPort", NULL, &returnTypeGUID, (LPBYTE)returnBuffer, &sizeOfReturnBuffer) != ERROR_SUCCESS))
262 WARN(": registry entry missing\n" );
263 *guid_port = CLSID_DirectMusicSynth;
264 return S_OK;
266 /* FIXME: Check return types to ensure we're interpreting data right */
267 MultiByteToWideChar(CP_ACP, 0, returnBuffer, -1, buff, sizeof(buff) / sizeof(WCHAR));
268 CLSIDFromString(buff, &defaultPortGUID);
269 *guid_port = defaultPortGUID;
271 return S_OK;
274 static HRESULT WINAPI IDirectMusic8Impl_SetDirectSound(LPDIRECTMUSIC8 iface, LPDIRECTSOUND dsound, HWND wnd)
276 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
278 FIXME("(%p)->(%p, %p): stub\n", This, dsound, wnd);
280 return S_OK;
283 static HRESULT WINAPI IDirectMusic8Impl_SetExternalMasterClock(LPDIRECTMUSIC8 iface, IReferenceClock* clock)
285 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
287 FIXME("(%p)->(%p): stub\n", This, clock);
289 return S_OK;
292 static const IDirectMusic8Vtbl DirectMusic8_Vtbl = {
293 IDirectMusic8Impl_QueryInterface,
294 IDirectMusic8Impl_AddRef,
295 IDirectMusic8Impl_Release,
296 IDirectMusic8Impl_EnumPort,
297 IDirectMusic8Impl_CreateMusicBuffer,
298 IDirectMusic8Impl_CreatePort,
299 IDirectMusic8Impl_EnumMasterClock,
300 IDirectMusic8Impl_GetMasterClock,
301 IDirectMusic8Impl_SetMasterClock,
302 IDirectMusic8Impl_Activate,
303 IDirectMusic8Impl_GetDefaultPort,
304 IDirectMusic8Impl_SetDirectSound,
305 IDirectMusic8Impl_SetExternalMasterClock
308 static void create_system_ports_list(IDirectMusic8Impl* object)
310 port_info * port;
311 const WCHAR emulated[] = {' ','[','E','m','u','l','a','t','e','d',']',0};
312 ULONG nb_ports;
313 ULONG nb_midi_out;
314 ULONG nb_midi_in;
315 MIDIOUTCAPSW caps_out;
316 MIDIINCAPSW caps_in;
317 IDirectMusicSynth8* synth;
318 HRESULT hr;
319 ULONG i;
321 TRACE("(%p)\n", object);
323 /* NOTE:
324 - it seems some native versions get the rest of devices through dmusic32.EnumLegacyDevices...*sigh*...which is undocumented
325 - should we enum wave devices ? Native does not seem to
328 nb_midi_out = midiOutGetNumDevs();
329 nb_midi_in = midiInGetNumDevs();
330 nb_ports = 1 /* midi mapper */ + nb_midi_out + nb_midi_in + 1 /* synth port */;
332 port = object->system_ports = HeapAlloc(GetProcessHeap(), 0, nb_ports * sizeof(port_info));
333 if (!object->system_ports)
334 return;
336 /* Fill common port caps for all winmm ports */
337 for (i = 0; i < (nb_ports - 1 /* synth port*/); i++)
339 object->system_ports[i].caps.dwSize = sizeof(DMUS_PORTCAPS);
340 object->system_ports[i].caps.dwType = DMUS_PORT_WINMM_DRIVER;
341 object->system_ports[i].caps.dwMemorySize = 0;
342 object->system_ports[i].caps.dwMaxChannelGroups = 1;
343 object->system_ports[i].caps.dwMaxVoices = 0;
344 object->system_ports[i].caps.dwMaxAudioChannels = 0;
345 object->system_ports[i].caps.dwEffectFlags = DMUS_EFFECT_NONE;
346 /* Fake port GUID */
347 object->system_ports[i].caps.guidPort = IID_IUnknown;
348 object->system_ports[i].caps.guidPort.Data1 = i + 1;
351 /* Fill midi mapper port info */
352 port->device = MIDI_MAPPER;
353 port->create = DMUSIC_CreateMidiOutPortImpl;
354 midiOutGetDevCapsW(MIDI_MAPPER, &caps_out, sizeof(caps_out));
355 strcpyW(port->caps.wszDescription, caps_out.szPname);
356 strcatW(port->caps.wszDescription, emulated);
357 port->caps.dwFlags = DMUS_PC_SHAREABLE;
358 port->caps.dwClass = DMUS_PC_OUTPUTCLASS;
359 port++;
361 /* Fill midi out port info */
362 for (i = 0; i < nb_midi_out; i++)
364 port->device = i;
365 port->create = DMUSIC_CreateMidiOutPortImpl;
366 midiOutGetDevCapsW(i, &caps_out, sizeof(caps_out));
367 strcpyW(port->caps.wszDescription, caps_out.szPname);
368 strcatW(port->caps.wszDescription, emulated);
369 port->caps.dwFlags = DMUS_PC_SHAREABLE | DMUS_PC_EXTERNAL;
370 port->caps.dwClass = DMUS_PC_OUTPUTCLASS;
371 port++;
374 /* Fill midi in port info */
375 for (i = 0; i < nb_midi_in; i++)
377 port->device = i;
378 port->create = DMUSIC_CreateMidiInPortImpl;
379 midiInGetDevCapsW(i, &caps_in, sizeof(caps_in));
380 strcpyW(port->caps.wszDescription, caps_in.szPname);
381 strcatW(port->caps.wszDescription, emulated);
382 port->caps.dwFlags = DMUS_PC_EXTERNAL;
383 port->caps.dwClass = DMUS_PC_INPUTCLASS;
384 port++;
387 /* Fill synth port info */
388 port->create = DMUSIC_CreateSynthPortImpl;
389 hr = CoCreateInstance(&CLSID_DirectMusicSynth, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicSynth8, (void**)&synth);
390 if (SUCCEEDED(hr))
392 port->caps.dwSize = sizeof(port->caps);
393 hr = IDirectMusicSynth8_GetPortCaps(synth, &port->caps);
394 IDirectMusicSynth8_Release(synth);
396 if (FAILED(hr))
397 nb_ports--;
399 object->nb_system_ports = nb_ports;
402 /* For ClassFactory */
403 HRESULT WINAPI DMUSIC_CreateDirectMusicImpl(LPCGUID riid, LPVOID* ret_iface, LPUNKNOWN unkouter)
405 IDirectMusic8Impl *dmusic;
406 HRESULT ret;
408 TRACE("(%p,%p,%p)\n", riid, ret_iface, unkouter);
410 *ret_iface = NULL;
412 dmusic = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusic8Impl));
413 if (!dmusic)
414 return E_OUTOFMEMORY;
416 dmusic->IDirectMusic8_iface.lpVtbl = &DirectMusic8_Vtbl;
417 dmusic->ref = 0; /* Will be inited by QueryInterface */
418 dmusic->pMasterClock = NULL;
419 dmusic->ppPorts = NULL;
420 dmusic->nrofports = 0;
421 ret = DMUSIC_CreateReferenceClockImpl(&IID_IReferenceClock, (LPVOID*)&dmusic->pMasterClock, NULL);
422 if (FAILED(ret)) {
423 HeapFree(GetProcessHeap(), 0, dmusic);
424 return ret;
427 ret = IDirectMusic8Impl_QueryInterface(&dmusic->IDirectMusic8_iface, riid, ret_iface);
428 if (FAILED(ret)) {
429 IReferenceClock_Release(&dmusic->pMasterClock->IReferenceClock_iface);
430 HeapFree(GetProcessHeap(), 0, dmusic);
431 return ret;
434 create_system_ports_list(dmusic);
436 return S_OK;