include: Add STORAGE_HOTPLUG_INFO structure.
[wine.git] / dlls / dmusic / dmusic.c
blob822c35ba616c1d84c12aafc82f1b6696c0b99f78
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 struct master_clock {
29 IReferenceClock IReferenceClock_iface;
30 LONG ref;
31 double freq;
32 REFERENCE_TIME last_time;
35 static inline struct master_clock *impl_from_IReferenceClock(IReferenceClock *iface)
37 return CONTAINING_RECORD(iface, struct master_clock, IReferenceClock_iface);
40 static HRESULT WINAPI master_IReferenceClock_QueryInterface(IReferenceClock *iface, REFIID riid,
41 void **ret_iface)
43 TRACE("(%p, %s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
45 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IReferenceClock))
46 *ret_iface = iface;
47 else {
48 WARN("no interface for %s\n", debugstr_dmguid(riid));
49 *ret_iface = NULL;
50 return E_NOINTERFACE;
53 IReferenceClock_AddRef(iface);
55 return S_OK;
58 static ULONG WINAPI master_IReferenceClock_AddRef(IReferenceClock *iface)
60 struct master_clock *This = impl_from_IReferenceClock(iface);
61 ULONG ref = InterlockedIncrement(&This->ref);
63 TRACE("(%p) ref = %lu\n", iface, ref);
65 return ref;
68 static ULONG WINAPI master_IReferenceClock_Release(IReferenceClock *iface)
70 struct master_clock *This = impl_from_IReferenceClock(iface);
71 ULONG ref = InterlockedDecrement(&This->ref);
73 TRACE("(%p) ref = %lu\n", iface, ref);
75 if (!ref)
76 free(This);
78 return ref;
81 static HRESULT WINAPI master_IReferenceClock_GetTime(IReferenceClock *iface,
82 REFERENCE_TIME *time)
84 struct master_clock *This = impl_from_IReferenceClock(iface);
85 LARGE_INTEGER counter;
86 HRESULT hr;
88 TRACE("(%p, %p)\n", iface, time);
90 QueryPerformanceCounter(&counter);
91 *time = counter.QuadPart * This->freq;
92 hr = (*time == This->last_time) ? S_FALSE : S_OK;
93 This->last_time = *time;
95 return hr;
98 static HRESULT WINAPI master_IReferenceClock_AdviseTime(IReferenceClock *iface, REFERENCE_TIME base,
99 REFERENCE_TIME offset, HEVENT event, DWORD_PTR *cookie)
101 FIXME("(%p, %I64d, %I64d, %#Ix, %p): stub\n", iface, base, offset, event, cookie);
102 return E_NOTIMPL;
105 static HRESULT WINAPI master_IReferenceClock_AdvisePeriodic(IReferenceClock *iface, REFERENCE_TIME start,
106 REFERENCE_TIME period, HSEMAPHORE semaphore, DWORD_PTR *cookie)
108 FIXME("(%p, %I64d, %I64d, %#Ix, %p): stub\n", iface, start, period, semaphore, cookie);
109 return E_NOTIMPL;
112 static HRESULT WINAPI master_IReferenceClock_Unadvise(IReferenceClock *iface, DWORD_PTR cookie)
114 FIXME("(%p, %#Ix): stub\n", iface, cookie);
115 return E_NOTIMPL;
118 static const IReferenceClockVtbl master_clock_vtbl = {
119 master_IReferenceClock_QueryInterface,
120 master_IReferenceClock_AddRef,
121 master_IReferenceClock_Release,
122 master_IReferenceClock_GetTime,
123 master_IReferenceClock_AdviseTime,
124 master_IReferenceClock_AdvisePeriodic,
125 master_IReferenceClock_Unadvise,
128 static HRESULT master_clock_create(IReferenceClock **clock)
130 struct master_clock *obj;
131 LARGE_INTEGER freq;
133 TRACE("(%p)\n", clock);
135 if (!(obj = calloc(1, sizeof(*obj))))
136 return E_OUTOFMEMORY;
138 obj->IReferenceClock_iface.lpVtbl = &master_clock_vtbl;
139 obj->ref = 1;
140 QueryPerformanceFrequency(&freq);
141 obj->freq = 10000000.0 / freq.QuadPart;
143 *clock = &obj->IReferenceClock_iface;
145 return S_OK;
148 static inline IDirectMusic8Impl *impl_from_IDirectMusic8(IDirectMusic8 *iface)
150 return CONTAINING_RECORD(iface, IDirectMusic8Impl, IDirectMusic8_iface);
153 /* IDirectMusic8Impl IUnknown part: */
154 static HRESULT WINAPI IDirectMusic8Impl_QueryInterface(LPDIRECTMUSIC8 iface, REFIID riid, LPVOID *ret_iface)
156 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
158 TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
160 if (IsEqualIID (riid, &IID_IUnknown) ||
161 IsEqualIID (riid, &IID_IDirectMusic) ||
162 IsEqualIID (riid, &IID_IDirectMusic2) ||
163 IsEqualIID (riid, &IID_IDirectMusic8))
165 IDirectMusic8_AddRef(iface);
166 *ret_iface = iface;
167 return S_OK;
170 *ret_iface = NULL;
172 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
174 return E_NOINTERFACE;
177 static ULONG WINAPI IDirectMusic8Impl_AddRef(LPDIRECTMUSIC8 iface)
179 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
180 ULONG ref = InterlockedIncrement(&This->ref);
182 TRACE("(%p): new ref = %lu\n", This, ref);
184 return ref;
187 static ULONG WINAPI IDirectMusic8Impl_Release(LPDIRECTMUSIC8 iface)
189 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
190 ULONG ref = InterlockedDecrement(&This->ref);
192 TRACE("(%p): new ref = %lu\n", This, ref);
194 if (!ref) {
195 IReferenceClock_Release(This->master_clock);
196 if (This->dsound)
197 IDirectSound_Release(This->dsound);
198 free(This->system_ports);
199 free(This->ports);
200 free(This);
203 return ref;
206 /* IDirectMusic8Impl IDirectMusic part: */
207 static HRESULT WINAPI IDirectMusic8Impl_EnumPort(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_PORTCAPS port_caps)
209 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
211 TRACE("(%p, %ld, %p)\n", This, index, port_caps);
213 if (!port_caps)
214 return E_POINTER;
216 if (index >= This->num_system_ports)
217 return S_FALSE;
219 *port_caps = This->system_ports[index].caps;
221 return S_OK;
224 static HRESULT WINAPI IDirectMusic8Impl_CreateMusicBuffer(LPDIRECTMUSIC8 iface, LPDMUS_BUFFERDESC buffer_desc, LPDIRECTMUSICBUFFER* buffer, LPUNKNOWN unkouter)
226 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
228 TRACE("(%p)->(%p, %p, %p)\n", This, buffer_desc, buffer, unkouter);
230 if (unkouter)
231 return CLASS_E_NOAGGREGATION;
233 if (!buffer_desc || !buffer)
234 return E_POINTER;
236 return DMUSIC_CreateDirectMusicBufferImpl(buffer_desc, (LPVOID)buffer);
239 static HRESULT WINAPI IDirectMusic8Impl_CreatePort(LPDIRECTMUSIC8 iface, REFCLSID rclsid_port, LPDMUS_PORTPARAMS port_params, LPDIRECTMUSICPORT* port, LPUNKNOWN unkouter)
241 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
242 int i;
243 DMUS_PORTCAPS port_caps;
244 IDirectMusicPort* new_port = NULL;
245 HRESULT hr;
246 GUID default_port;
247 const GUID *request_port = rclsid_port;
249 TRACE("(%p)->(%s, %p, %p, %p)\n", This, debugstr_dmguid(rclsid_port), port_params, port, unkouter);
251 if (!rclsid_port || !port)
252 return E_POINTER;
253 if (!port_params)
254 return E_INVALIDARG;
255 if (unkouter)
256 return CLASS_E_NOAGGREGATION;
257 if (!This->dsound)
258 return DMUS_E_DSOUND_NOT_SET;
260 if (TRACE_ON(dmusic))
261 dump_DMUS_PORTPARAMS(port_params);
263 ZeroMemory(&port_caps, sizeof(DMUS_PORTCAPS));
264 port_caps.dwSize = sizeof(DMUS_PORTCAPS);
266 if (IsEqualGUID(request_port, &GUID_NULL)) {
267 hr = IDirectMusic8_GetDefaultPort(iface, &default_port);
268 if(FAILED(hr))
269 return hr;
270 request_port = &default_port;
273 for (i = 0; S_FALSE != IDirectMusic8Impl_EnumPort(iface, i, &port_caps); i++) {
274 if (IsEqualCLSID(request_port, &port_caps.guidPort)) {
275 hr = This->system_ports[i].create(This, port_params, &port_caps, &new_port);
276 if (FAILED(hr)) {
277 *port = NULL;
278 return hr;
280 This->num_ports++;
281 This->ports = realloc(This->ports, sizeof(*This->ports) * This->num_ports);
282 This->ports[This->num_ports - 1] = new_port;
283 *port = new_port;
284 return S_OK;
288 return E_NOINTERFACE;
291 void dmusic_remove_port(IDirectMusic8Impl *dmusic, IDirectMusicPort *port)
293 BOOL found = FALSE;
294 int i;
296 TRACE("Removing port %p.\n", port);
298 for (i = 0; i < dmusic->num_ports; i++)
300 if (dmusic->ports[i] == port) {
301 found = TRUE;
302 break;
306 if (!found)
308 ERR("Port %p not found in ports array.\n", port);
309 return;
312 if (!--dmusic->num_ports) {
313 free(dmusic->ports);
314 dmusic->ports = NULL;
315 return;
318 memmove(&dmusic->ports[i], &dmusic->ports[i + 1],
319 (dmusic->num_ports - i) * sizeof(*dmusic->ports));
320 dmusic->ports = realloc(dmusic->ports, sizeof(*dmusic->ports) * dmusic->num_ports);
323 static HRESULT WINAPI IDirectMusic8Impl_EnumMasterClock(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_CLOCKINFO clock_info)
325 TRACE("(%p, %ld, %p)\n", iface, index, clock_info);
327 if (!clock_info)
328 return E_POINTER;
330 if (index > 1)
331 return S_FALSE;
333 if (!index)
335 static const GUID guid_system_clock = { 0x58d58419, 0x71b4, 0x11d1, { 0xa7, 0x4c, 0x00, 0x00, 0xf8, 0x75, 0xac, 0x12 } };
337 clock_info->ctType = 0;
338 clock_info->guidClock = guid_system_clock;
339 lstrcpyW(clock_info->wszDescription, L"System Clock");
341 else
343 static const GUID guid_dsound_clock = { 0x58d58420, 0x71b4, 0x11d1, { 0xa7, 0x4c, 0x00, 0x00, 0xf8, 0x75, 0xac, 0x12 } };
345 clock_info->ctType = 0;
346 clock_info->guidClock = guid_dsound_clock;
347 lstrcpyW(clock_info->wszDescription, L"DirectSound Clock");
350 return S_OK;
353 static HRESULT WINAPI IDirectMusic8Impl_GetMasterClock(LPDIRECTMUSIC8 iface, LPGUID guid_clock, IReferenceClock** reference_clock)
355 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
357 TRACE("(%p)->(%p, %p)\n", This, guid_clock, reference_clock);
359 if (guid_clock)
360 *guid_clock = GUID_NULL;
361 if (reference_clock) {
362 *reference_clock = This->master_clock;
363 IReferenceClock_AddRef(*reference_clock);
366 return S_OK;
369 static HRESULT WINAPI IDirectMusic8Impl_SetMasterClock(LPDIRECTMUSIC8 iface, REFGUID rguidClock)
371 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
373 FIXME("(%p)->(%s): stub\n", This, debugstr_dmguid(rguidClock));
375 return S_OK;
378 static HRESULT WINAPI IDirectMusic8Impl_Activate(LPDIRECTMUSIC8 iface, BOOL enable)
380 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
381 int i;
382 HRESULT hr;
384 TRACE("(%p)->(%u)\n", This, enable);
386 for (i = 0; i < This->num_ports; i++)
388 hr = IDirectMusicPort_Activate(This->ports[i], enable);
389 if (FAILED(hr))
390 return hr;
393 return S_OK;
396 static HRESULT WINAPI IDirectMusic8Impl_GetDefaultPort(LPDIRECTMUSIC8 iface, LPGUID guid_port)
398 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
399 HKEY hkGUID;
400 DWORD returnTypeGUID, sizeOfReturnBuffer = 50;
401 char returnBuffer[51];
402 GUID defaultPortGUID;
403 WCHAR buff[51];
405 TRACE("(%p)->(%p)\n", This, guid_port);
407 if ((RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic\\Defaults" , 0, KEY_READ, &hkGUID) != ERROR_SUCCESS) ||
408 (RegQueryValueExA(hkGUID, "DefaultOutputPort", NULL, &returnTypeGUID, (LPBYTE)returnBuffer, &sizeOfReturnBuffer) != ERROR_SUCCESS))
410 WARN(": registry entry missing\n" );
411 *guid_port = CLSID_DirectMusicSynth;
412 return S_OK;
414 /* FIXME: Check return types to ensure we're interpreting data right */
415 MultiByteToWideChar(CP_ACP, 0, returnBuffer, -1, buff, ARRAY_SIZE(buff));
416 CLSIDFromString(buff, &defaultPortGUID);
417 *guid_port = defaultPortGUID;
419 return S_OK;
422 static HRESULT WINAPI IDirectMusic8Impl_SetDirectSound(IDirectMusic8 *iface, IDirectSound *dsound,
423 HWND hwnd)
425 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
426 HRESULT hr;
427 int i;
429 TRACE("(%p)->(%p, %p)\n", This, dsound, hwnd);
431 for (i = 0; i < This->num_ports; i++)
433 hr = IDirectMusicPort_SetDirectSound(This->ports[i], NULL, NULL);
434 if (FAILED(hr))
435 return hr;
438 if (This->dsound)
439 IDirectSound_Release(This->dsound);
441 if (!dsound) {
442 hr = DirectSoundCreate8(NULL, (IDirectSound8 **)&This->dsound, NULL);
443 if (FAILED(hr))
444 return hr;
445 hr = IDirectSound_SetCooperativeLevel(This->dsound, hwnd ? hwnd : GetForegroundWindow(),
446 DSSCL_PRIORITY);
447 if (FAILED(hr)) {
448 IDirectSound_Release(This->dsound);
449 This->dsound = NULL;
451 return hr;
454 IDirectSound_AddRef(dsound);
455 This->dsound = dsound;
457 return S_OK;
460 static HRESULT WINAPI IDirectMusic8Impl_SetExternalMasterClock(LPDIRECTMUSIC8 iface, IReferenceClock* clock)
462 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
464 FIXME("(%p)->(%p): stub\n", This, clock);
466 return S_OK;
469 static const IDirectMusic8Vtbl DirectMusic8_Vtbl = {
470 IDirectMusic8Impl_QueryInterface,
471 IDirectMusic8Impl_AddRef,
472 IDirectMusic8Impl_Release,
473 IDirectMusic8Impl_EnumPort,
474 IDirectMusic8Impl_CreateMusicBuffer,
475 IDirectMusic8Impl_CreatePort,
476 IDirectMusic8Impl_EnumMasterClock,
477 IDirectMusic8Impl_GetMasterClock,
478 IDirectMusic8Impl_SetMasterClock,
479 IDirectMusic8Impl_Activate,
480 IDirectMusic8Impl_GetDefaultPort,
481 IDirectMusic8Impl_SetDirectSound,
482 IDirectMusic8Impl_SetExternalMasterClock
485 static void create_system_ports_list(IDirectMusic8Impl* object)
487 static const WCHAR emulated[] = L" [Emulated]";
488 port_info * port;
489 ULONG nb_ports;
490 ULONG nb_midi_out;
491 ULONG nb_midi_in;
492 MIDIOUTCAPSW caps_out;
493 MIDIINCAPSW caps_in;
494 IDirectMusicSynth8* synth;
495 HRESULT hr;
496 ULONG i;
498 TRACE("(%p)\n", object);
500 /* NOTE:
501 - it seems some native versions get the rest of devices through dmusic32.EnumLegacyDevices...*sigh*...which is undocumented
502 - should we enum wave devices ? Native does not seem to
505 nb_midi_out = midiOutGetNumDevs();
506 nb_midi_in = midiInGetNumDevs();
507 nb_ports = 1 /* midi mapper */ + nb_midi_out + nb_midi_in + 1 /* synth port */;
509 port = object->system_ports = malloc(nb_ports * sizeof(port_info));
510 if (!object->system_ports)
511 return;
513 /* Fill common port caps for all winmm ports */
514 for (i = 0; i < (nb_ports - 1 /* synth port*/); i++)
516 object->system_ports[i].caps.dwSize = sizeof(DMUS_PORTCAPS);
517 object->system_ports[i].caps.dwType = DMUS_PORT_WINMM_DRIVER;
518 object->system_ports[i].caps.dwMemorySize = 0;
519 object->system_ports[i].caps.dwMaxChannelGroups = 1;
520 object->system_ports[i].caps.dwMaxVoices = 0;
521 object->system_ports[i].caps.dwMaxAudioChannels = 0;
522 object->system_ports[i].caps.dwEffectFlags = DMUS_EFFECT_NONE;
523 /* Fake port GUID */
524 object->system_ports[i].caps.guidPort = IID_IUnknown;
525 object->system_ports[i].caps.guidPort.Data1 = i + 1;
528 /* Fill midi mapper port info */
529 port->device = MIDI_MAPPER;
530 port->create = midi_out_port_create;
531 midiOutGetDevCapsW(MIDI_MAPPER, &caps_out, sizeof(caps_out));
532 lstrcpyW(port->caps.wszDescription, caps_out.szPname);
533 lstrcatW(port->caps.wszDescription, emulated);
534 port->caps.dwFlags = DMUS_PC_SHAREABLE;
535 port->caps.dwClass = DMUS_PC_OUTPUTCLASS;
536 port++;
538 /* Fill midi out port info */
539 for (i = 0; i < nb_midi_out; i++)
541 port->device = i;
542 port->create = midi_out_port_create;
543 midiOutGetDevCapsW(i, &caps_out, sizeof(caps_out));
544 lstrcpyW(port->caps.wszDescription, caps_out.szPname);
545 lstrcatW(port->caps.wszDescription, emulated);
546 port->caps.dwFlags = DMUS_PC_SHAREABLE | DMUS_PC_EXTERNAL;
547 port->caps.dwClass = DMUS_PC_OUTPUTCLASS;
548 port++;
551 /* Fill midi in port info */
552 for (i = 0; i < nb_midi_in; i++)
554 port->device = i;
555 port->create = midi_in_port_create;
556 midiInGetDevCapsW(i, &caps_in, sizeof(caps_in));
557 lstrcpyW(port->caps.wszDescription, caps_in.szPname);
558 lstrcatW(port->caps.wszDescription, emulated);
559 port->caps.dwFlags = DMUS_PC_EXTERNAL;
560 port->caps.dwClass = DMUS_PC_INPUTCLASS;
561 port++;
564 /* Fill synth port info */
565 port->create = synth_port_create;
566 hr = CoCreateInstance(&CLSID_DirectMusicSynth, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicSynth8, (void**)&synth);
567 if (SUCCEEDED(hr))
569 port->caps.dwSize = sizeof(port->caps);
570 hr = IDirectMusicSynth8_GetPortCaps(synth, &port->caps);
571 IDirectMusicSynth8_Release(synth);
573 if (FAILED(hr))
574 nb_ports--;
576 object->num_system_ports = nb_ports;
579 HRESULT music_create(IUnknown **ret_iface)
581 IDirectMusic8Impl *dmusic;
582 HRESULT ret;
584 TRACE("(%p)\n", ret_iface);
586 *ret_iface = NULL;
587 if (!(dmusic = calloc(1, sizeof(*dmusic)))) return E_OUTOFMEMORY;
588 dmusic->IDirectMusic8_iface.lpVtbl = &DirectMusic8_Vtbl;
589 dmusic->ref = 1;
590 ret = master_clock_create(&dmusic->master_clock);
591 if (FAILED(ret)) {
592 free(dmusic);
593 return ret;
596 create_system_ports_list(dmusic);
598 TRACE("Created DirectMusic %p\n", dmusic);
599 *ret_iface = (IUnknown *)&dmusic->IDirectMusic8_iface;
600 return S_OK;