widl: Always check the runtimeclass interfaces presence.
[wine/zf.git] / dlls / dmusic / dmusic.c
blob8dc9e788858b66edcf1eeb43e5886a9d782883fb
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"
25 #include "dmobject.h"
26 #include "wine/heap.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
30 struct master_clock {
31 IReferenceClock IReferenceClock_iface;
32 LONG ref;
33 double freq;
34 REFERENCE_TIME last_time;
37 static inline struct master_clock *impl_from_IReferenceClock(IReferenceClock *iface)
39 return CONTAINING_RECORD(iface, struct master_clock, IReferenceClock_iface);
42 static HRESULT WINAPI master_IReferenceClock_QueryInterface(IReferenceClock *iface, REFIID riid,
43 void **ret_iface)
45 TRACE("(%p, %s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
47 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IReferenceClock))
48 *ret_iface = iface;
49 else {
50 WARN("no interface for %s\n", debugstr_dmguid(riid));
51 *ret_iface = NULL;
52 return E_NOINTERFACE;
55 IReferenceClock_AddRef(iface);
57 return S_OK;
60 static ULONG WINAPI master_IReferenceClock_AddRef(IReferenceClock *iface)
62 struct master_clock *This = impl_from_IReferenceClock(iface);
63 ULONG ref = InterlockedIncrement(&This->ref);
65 TRACE("(%p) ref = %u\n", iface, ref);
67 return ref;
70 static ULONG WINAPI master_IReferenceClock_Release(IReferenceClock *iface)
72 struct master_clock *This = impl_from_IReferenceClock(iface);
73 ULONG ref = InterlockedDecrement(&This->ref);
75 TRACE("(%p) ref = %u\n", iface, ref);
77 if (!ref)
78 heap_free(This);
80 return ref;
83 static HRESULT WINAPI master_IReferenceClock_GetTime(IReferenceClock *iface,
84 REFERENCE_TIME *time)
86 struct master_clock *This = impl_from_IReferenceClock(iface);
87 LARGE_INTEGER counter;
88 HRESULT hr;
90 TRACE("(%p, %p)\n", iface, time);
92 QueryPerformanceCounter(&counter);
93 *time = counter.QuadPart * This->freq;
94 hr = (*time == This->last_time) ? S_FALSE : S_OK;
95 This->last_time = *time;
97 return hr;
100 static HRESULT WINAPI master_IReferenceClock_AdviseTime(IReferenceClock *iface,
101 REFERENCE_TIME base, REFERENCE_TIME offset, HANDLE event, DWORD *cookie)
103 TRACE("(%p, %s, %s, %p, %p): method not implemented\n", iface, wine_dbgstr_longlong(base),
104 wine_dbgstr_longlong(offset), event, cookie);
105 return E_NOTIMPL;
108 static HRESULT WINAPI master_IReferenceClock_AdvisePeriodic(IReferenceClock *iface,
109 REFERENCE_TIME start, REFERENCE_TIME period, HANDLE semaphore, DWORD *cookie)
111 TRACE("(%p, %s, %s, %p, %p): method not implemented\n", iface, wine_dbgstr_longlong(start),
112 wine_dbgstr_longlong(period), semaphore, cookie);
113 return E_NOTIMPL;
116 static HRESULT WINAPI master_IReferenceClock_Unadvise(IReferenceClock *iface, DWORD cookie)
118 TRACE("(%p, %#x): method not implemented\n", iface, cookie);
119 return E_NOTIMPL;
122 static const IReferenceClockVtbl master_clock_vtbl = {
123 master_IReferenceClock_QueryInterface,
124 master_IReferenceClock_AddRef,
125 master_IReferenceClock_Release,
126 master_IReferenceClock_GetTime,
127 master_IReferenceClock_AdviseTime,
128 master_IReferenceClock_AdvisePeriodic,
129 master_IReferenceClock_Unadvise,
132 static HRESULT master_clock_create(IReferenceClock **clock)
134 struct master_clock *obj;
135 LARGE_INTEGER freq;
137 TRACE("(%p)\n", clock);
139 if (!(obj = heap_alloc_zero(sizeof(*obj))))
140 return E_OUTOFMEMORY;
142 obj->IReferenceClock_iface.lpVtbl = &master_clock_vtbl;
143 obj->ref = 1;
144 QueryPerformanceFrequency(&freq);
145 obj->freq = 10000000.0 / freq.QuadPart;
147 *clock = &obj->IReferenceClock_iface;
149 return S_OK;
152 static inline IDirectMusic8Impl *impl_from_IDirectMusic8(IDirectMusic8 *iface)
154 return CONTAINING_RECORD(iface, IDirectMusic8Impl, IDirectMusic8_iface);
157 /* IDirectMusic8Impl IUnknown part: */
158 static HRESULT WINAPI IDirectMusic8Impl_QueryInterface(LPDIRECTMUSIC8 iface, REFIID riid, LPVOID *ret_iface)
160 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
162 TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
164 if (IsEqualIID (riid, &IID_IUnknown) ||
165 IsEqualIID (riid, &IID_IDirectMusic) ||
166 IsEqualIID (riid, &IID_IDirectMusic2) ||
167 IsEqualIID (riid, &IID_IDirectMusic8))
169 IDirectMusic8_AddRef(iface);
170 *ret_iface = iface;
171 return S_OK;
174 *ret_iface = NULL;
176 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
178 return E_NOINTERFACE;
181 static ULONG WINAPI IDirectMusic8Impl_AddRef(LPDIRECTMUSIC8 iface)
183 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
184 ULONG ref = InterlockedIncrement(&This->ref);
186 TRACE("(%p)->(): new ref = %u\n", This, ref);
188 return ref;
191 static ULONG WINAPI IDirectMusic8Impl_Release(LPDIRECTMUSIC8 iface)
193 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
194 ULONG ref = InterlockedDecrement(&This->ref);
196 TRACE("(%p)->(): new ref = %u\n", This, ref);
198 if (!ref) {
199 IReferenceClock_Release(This->master_clock);
200 if (This->dsound)
201 IDirectSound_Release(This->dsound);
202 HeapFree(GetProcessHeap(), 0, This->system_ports);
203 HeapFree(GetProcessHeap(), 0, This->ports);
204 HeapFree(GetProcessHeap(), 0, This);
205 DMUSIC_UnlockModule();
208 return ref;
211 /* IDirectMusic8Impl IDirectMusic part: */
212 static HRESULT WINAPI IDirectMusic8Impl_EnumPort(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_PORTCAPS port_caps)
214 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
216 TRACE("(%p, %d, %p)\n", This, index, port_caps);
218 if (!port_caps)
219 return E_POINTER;
221 if (index >= This->num_system_ports)
222 return S_FALSE;
224 *port_caps = This->system_ports[index].caps;
226 return S_OK;
229 static HRESULT WINAPI IDirectMusic8Impl_CreateMusicBuffer(LPDIRECTMUSIC8 iface, LPDMUS_BUFFERDESC buffer_desc, LPDIRECTMUSICBUFFER* buffer, LPUNKNOWN unkouter)
231 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
233 TRACE("(%p)->(%p, %p, %p)\n", This, buffer_desc, buffer, unkouter);
235 if (unkouter)
236 return CLASS_E_NOAGGREGATION;
238 if (!buffer_desc || !buffer)
239 return E_POINTER;
241 return DMUSIC_CreateDirectMusicBufferImpl(buffer_desc, (LPVOID)buffer);
244 static HRESULT WINAPI IDirectMusic8Impl_CreatePort(LPDIRECTMUSIC8 iface, REFCLSID rclsid_port, LPDMUS_PORTPARAMS port_params, LPDIRECTMUSICPORT* port, LPUNKNOWN unkouter)
246 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
247 int i;
248 DMUS_PORTCAPS port_caps;
249 IDirectMusicPort* new_port = NULL;
250 HRESULT hr;
251 GUID default_port;
252 const GUID *request_port = rclsid_port;
254 TRACE("(%p)->(%s, %p, %p, %p)\n", This, debugstr_dmguid(rclsid_port), port_params, port, unkouter);
256 if (!rclsid_port || !port)
257 return E_POINTER;
258 if (!port_params)
259 return E_INVALIDARG;
260 if (unkouter)
261 return CLASS_E_NOAGGREGATION;
262 if (!This->dsound)
263 return DMUS_E_DSOUND_NOT_SET;
265 if (TRACE_ON(dmusic))
266 dump_DMUS_PORTPARAMS(port_params);
268 ZeroMemory(&port_caps, sizeof(DMUS_PORTCAPS));
269 port_caps.dwSize = sizeof(DMUS_PORTCAPS);
271 if (IsEqualGUID(request_port, &GUID_NULL)) {
272 hr = IDirectMusic8_GetDefaultPort(iface, &default_port);
273 if(FAILED(hr))
274 return hr;
275 request_port = &default_port;
278 for (i = 0; S_FALSE != IDirectMusic8Impl_EnumPort(iface, i, &port_caps); i++) {
279 if (IsEqualCLSID(request_port, &port_caps.guidPort)) {
280 hr = This->system_ports[i].create(This, port_params, &port_caps, &new_port);
281 if (FAILED(hr)) {
282 *port = NULL;
283 return hr;
285 This->num_ports++;
286 if (!This->ports)
287 This->ports = HeapAlloc(GetProcessHeap(), 0,
288 sizeof(*This->ports) * This->num_ports);
289 else
290 This->ports = HeapReAlloc(GetProcessHeap(), 0, This->ports,
291 sizeof(*This->ports) * This->num_ports);
292 This->ports[This->num_ports - 1] = new_port;
293 *port = new_port;
294 return S_OK;
298 return E_NOINTERFACE;
301 void dmusic_remove_port(IDirectMusic8Impl *dmusic, IDirectMusicPort *port)
303 BOOL found = FALSE;
304 int i;
306 TRACE("Removing port %p.\n", port);
308 for (i = 0; i < dmusic->num_ports; i++)
310 if (dmusic->ports[i] == port) {
311 found = TRUE;
312 break;
316 if (!found)
318 ERR("Port %p not found in ports array.\n", port);
319 return;
322 if (!--dmusic->num_ports) {
323 HeapFree(GetProcessHeap(), 0, dmusic->ports);
324 dmusic->ports = NULL;
325 return;
328 memmove(&dmusic->ports[i], &dmusic->ports[i + 1],
329 (dmusic->num_ports - i) * sizeof(*dmusic->ports));
330 dmusic->ports = HeapReAlloc(GetProcessHeap(), 0, dmusic->ports,
331 sizeof(*dmusic->ports) * dmusic->num_ports);
334 static HRESULT WINAPI IDirectMusic8Impl_EnumMasterClock(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_CLOCKINFO clock_info)
336 TRACE("(%p)->(%d, %p)\n", iface, index, clock_info);
338 if (!clock_info)
339 return E_POINTER;
341 if (index > 1)
342 return S_FALSE;
344 if (!index)
346 static const GUID guid_system_clock = { 0x58d58419, 0x71b4, 0x11d1, { 0xa7, 0x4c, 0x00, 0x00, 0xf8, 0x75, 0xac, 0x12 } };
348 clock_info->ctType = 0;
349 clock_info->guidClock = guid_system_clock;
350 lstrcpyW(clock_info->wszDescription, L"System Clock");
352 else
354 static const GUID guid_dsound_clock = { 0x58d58420, 0x71b4, 0x11d1, { 0xa7, 0x4c, 0x00, 0x00, 0xf8, 0x75, 0xac, 0x12 } };
356 clock_info->ctType = 0;
357 clock_info->guidClock = guid_dsound_clock;
358 lstrcpyW(clock_info->wszDescription, L"DirectSound Clock");
361 return S_OK;
364 static HRESULT WINAPI IDirectMusic8Impl_GetMasterClock(LPDIRECTMUSIC8 iface, LPGUID guid_clock, IReferenceClock** reference_clock)
366 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
368 TRACE("(%p)->(%p, %p)\n", This, guid_clock, reference_clock);
370 if (guid_clock)
371 *guid_clock = GUID_NULL;
372 if (reference_clock) {
373 *reference_clock = This->master_clock;
374 IReferenceClock_AddRef(*reference_clock);
377 return S_OK;
380 static HRESULT WINAPI IDirectMusic8Impl_SetMasterClock(LPDIRECTMUSIC8 iface, REFGUID rguidClock)
382 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
384 FIXME("(%p)->(%s): stub\n", This, debugstr_dmguid(rguidClock));
386 return S_OK;
389 static HRESULT WINAPI IDirectMusic8Impl_Activate(LPDIRECTMUSIC8 iface, BOOL enable)
391 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
392 int i;
393 HRESULT hr;
395 TRACE("(%p)->(%u)\n", This, enable);
397 for (i = 0; i < This->num_ports; i++)
399 hr = IDirectMusicPort_Activate(This->ports[i], enable);
400 if (FAILED(hr))
401 return hr;
404 return S_OK;
407 static HRESULT WINAPI IDirectMusic8Impl_GetDefaultPort(LPDIRECTMUSIC8 iface, LPGUID guid_port)
409 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
410 HKEY hkGUID;
411 DWORD returnTypeGUID, sizeOfReturnBuffer = 50;
412 char returnBuffer[51];
413 GUID defaultPortGUID;
414 WCHAR buff[51];
416 TRACE("(%p)->(%p)\n", This, guid_port);
418 if ((RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic\\Defaults" , 0, KEY_READ, &hkGUID) != ERROR_SUCCESS) ||
419 (RegQueryValueExA(hkGUID, "DefaultOutputPort", NULL, &returnTypeGUID, (LPBYTE)returnBuffer, &sizeOfReturnBuffer) != ERROR_SUCCESS))
421 WARN(": registry entry missing\n" );
422 *guid_port = CLSID_DirectMusicSynth;
423 return S_OK;
425 /* FIXME: Check return types to ensure we're interpreting data right */
426 MultiByteToWideChar(CP_ACP, 0, returnBuffer, -1, buff, ARRAY_SIZE(buff));
427 CLSIDFromString(buff, &defaultPortGUID);
428 *guid_port = defaultPortGUID;
430 return S_OK;
433 static HRESULT WINAPI IDirectMusic8Impl_SetDirectSound(IDirectMusic8 *iface, IDirectSound *dsound,
434 HWND hwnd)
436 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
437 HRESULT hr;
438 int i;
440 TRACE("(%p)->(%p, %p)\n", This, dsound, hwnd);
442 for (i = 0; i < This->num_ports; i++)
444 hr = IDirectMusicPort_SetDirectSound(This->ports[i], NULL, NULL);
445 if (FAILED(hr))
446 return hr;
449 if (This->dsound)
450 IDirectSound_Release(This->dsound);
452 if (!dsound) {
453 hr = DirectSoundCreate8(NULL, (IDirectSound8 **)&This->dsound, NULL);
454 if (FAILED(hr))
455 return hr;
456 hr = IDirectSound_SetCooperativeLevel(This->dsound, hwnd ? hwnd : GetForegroundWindow(),
457 DSSCL_PRIORITY);
458 if (FAILED(hr)) {
459 IDirectSound_Release(This->dsound);
460 This->dsound = NULL;
462 return hr;
465 IDirectSound_AddRef(dsound);
466 This->dsound = dsound;
468 return S_OK;
471 static HRESULT WINAPI IDirectMusic8Impl_SetExternalMasterClock(LPDIRECTMUSIC8 iface, IReferenceClock* clock)
473 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
475 FIXME("(%p)->(%p): stub\n", This, clock);
477 return S_OK;
480 static const IDirectMusic8Vtbl DirectMusic8_Vtbl = {
481 IDirectMusic8Impl_QueryInterface,
482 IDirectMusic8Impl_AddRef,
483 IDirectMusic8Impl_Release,
484 IDirectMusic8Impl_EnumPort,
485 IDirectMusic8Impl_CreateMusicBuffer,
486 IDirectMusic8Impl_CreatePort,
487 IDirectMusic8Impl_EnumMasterClock,
488 IDirectMusic8Impl_GetMasterClock,
489 IDirectMusic8Impl_SetMasterClock,
490 IDirectMusic8Impl_Activate,
491 IDirectMusic8Impl_GetDefaultPort,
492 IDirectMusic8Impl_SetDirectSound,
493 IDirectMusic8Impl_SetExternalMasterClock
496 static void create_system_ports_list(IDirectMusic8Impl* object)
498 static const WCHAR emulated[] = L" [Emulated]";
499 port_info * port;
500 ULONG nb_ports;
501 ULONG nb_midi_out;
502 ULONG nb_midi_in;
503 MIDIOUTCAPSW caps_out;
504 MIDIINCAPSW caps_in;
505 IDirectMusicSynth8* synth;
506 HRESULT hr;
507 ULONG i;
509 TRACE("(%p)\n", object);
511 /* NOTE:
512 - it seems some native versions get the rest of devices through dmusic32.EnumLegacyDevices...*sigh*...which is undocumented
513 - should we enum wave devices ? Native does not seem to
516 nb_midi_out = midiOutGetNumDevs();
517 nb_midi_in = midiInGetNumDevs();
518 nb_ports = 1 /* midi mapper */ + nb_midi_out + nb_midi_in + 1 /* synth port */;
520 port = object->system_ports = HeapAlloc(GetProcessHeap(), 0, nb_ports * sizeof(port_info));
521 if (!object->system_ports)
522 return;
524 /* Fill common port caps for all winmm ports */
525 for (i = 0; i < (nb_ports - 1 /* synth port*/); i++)
527 object->system_ports[i].caps.dwSize = sizeof(DMUS_PORTCAPS);
528 object->system_ports[i].caps.dwType = DMUS_PORT_WINMM_DRIVER;
529 object->system_ports[i].caps.dwMemorySize = 0;
530 object->system_ports[i].caps.dwMaxChannelGroups = 1;
531 object->system_ports[i].caps.dwMaxVoices = 0;
532 object->system_ports[i].caps.dwMaxAudioChannels = 0;
533 object->system_ports[i].caps.dwEffectFlags = DMUS_EFFECT_NONE;
534 /* Fake port GUID */
535 object->system_ports[i].caps.guidPort = IID_IUnknown;
536 object->system_ports[i].caps.guidPort.Data1 = i + 1;
539 /* Fill midi mapper port info */
540 port->device = MIDI_MAPPER;
541 port->create = midi_out_port_create;
542 midiOutGetDevCapsW(MIDI_MAPPER, &caps_out, sizeof(caps_out));
543 lstrcpyW(port->caps.wszDescription, caps_out.szPname);
544 lstrcatW(port->caps.wszDescription, emulated);
545 port->caps.dwFlags = DMUS_PC_SHAREABLE;
546 port->caps.dwClass = DMUS_PC_OUTPUTCLASS;
547 port++;
549 /* Fill midi out port info */
550 for (i = 0; i < nb_midi_out; i++)
552 port->device = i;
553 port->create = midi_out_port_create;
554 midiOutGetDevCapsW(i, &caps_out, sizeof(caps_out));
555 lstrcpyW(port->caps.wszDescription, caps_out.szPname);
556 lstrcatW(port->caps.wszDescription, emulated);
557 port->caps.dwFlags = DMUS_PC_SHAREABLE | DMUS_PC_EXTERNAL;
558 port->caps.dwClass = DMUS_PC_OUTPUTCLASS;
559 port++;
562 /* Fill midi in port info */
563 for (i = 0; i < nb_midi_in; i++)
565 port->device = i;
566 port->create = midi_in_port_create;
567 midiInGetDevCapsW(i, &caps_in, sizeof(caps_in));
568 lstrcpyW(port->caps.wszDescription, caps_in.szPname);
569 lstrcatW(port->caps.wszDescription, emulated);
570 port->caps.dwFlags = DMUS_PC_EXTERNAL;
571 port->caps.dwClass = DMUS_PC_INPUTCLASS;
572 port++;
575 /* Fill synth port info */
576 port->create = synth_port_create;
577 hr = CoCreateInstance(&CLSID_DirectMusicSynth, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicSynth8, (void**)&synth);
578 if (SUCCEEDED(hr))
580 port->caps.dwSize = sizeof(port->caps);
581 hr = IDirectMusicSynth8_GetPortCaps(synth, &port->caps);
582 IDirectMusicSynth8_Release(synth);
584 if (FAILED(hr))
585 nb_ports--;
587 object->num_system_ports = nb_ports;
590 /* For ClassFactory */
591 HRESULT WINAPI DMUSIC_CreateDirectMusicImpl(LPCGUID riid, LPVOID* ret_iface, LPUNKNOWN unkouter)
593 IDirectMusic8Impl *dmusic;
594 HRESULT ret;
596 TRACE("(%s, %p, %p)\n", debugstr_guid(riid), ret_iface, unkouter);
598 *ret_iface = NULL;
599 if (unkouter)
600 return CLASS_E_NOAGGREGATION;
602 dmusic = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusic8Impl));
603 if (!dmusic)
604 return E_OUTOFMEMORY;
606 dmusic->IDirectMusic8_iface.lpVtbl = &DirectMusic8_Vtbl;
607 dmusic->ref = 1;
608 ret = master_clock_create(&dmusic->master_clock);
609 if (FAILED(ret)) {
610 HeapFree(GetProcessHeap(), 0, dmusic);
611 return ret;
614 create_system_ports_list(dmusic);
616 DMUSIC_LockModule();
617 ret = IDirectMusic8Impl_QueryInterface(&dmusic->IDirectMusic8_iface, riid, ret_iface);
618 IDirectMusic8_Release(&dmusic->IDirectMusic8_iface);
620 return ret;