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
);
29 IReferenceClock IReferenceClock_iface
;
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
,
43 TRACE("(%p, %s, %p)\n", iface
, debugstr_dmguid(riid
), ret_iface
);
45 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IReferenceClock
))
48 WARN("no interface for %s\n", debugstr_dmguid(riid
));
53 IReferenceClock_AddRef(iface
);
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
);
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
);
81 static HRESULT WINAPI
master_IReferenceClock_GetTime(IReferenceClock
*iface
,
84 struct master_clock
*This
= impl_from_IReferenceClock(iface
);
85 LARGE_INTEGER counter
;
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
;
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
);
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
);
112 static HRESULT WINAPI
master_IReferenceClock_Unadvise(IReferenceClock
*iface
, DWORD_PTR cookie
)
114 FIXME("(%p, %#Ix): stub\n", iface
, cookie
);
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
;
133 TRACE("(%p)\n", clock
);
135 if (!(obj
= calloc(1, sizeof(*obj
))))
136 return E_OUTOFMEMORY
;
138 obj
->IReferenceClock_iface
.lpVtbl
= &master_clock_vtbl
;
140 QueryPerformanceFrequency(&freq
);
141 obj
->freq
= 10000000.0 / freq
.QuadPart
;
143 *clock
= &obj
->IReferenceClock_iface
;
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
);
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
);
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
);
195 IReferenceClock_Release(This
->master_clock
);
197 IDirectSound_Release(This
->dsound
);
198 free(This
->system_ports
);
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
);
216 if (index
>= This
->num_system_ports
)
219 *port_caps
= This
->system_ports
[index
].caps
;
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
);
231 return CLASS_E_NOAGGREGATION
;
233 if (!buffer_desc
|| !buffer
)
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
);
243 DMUS_PORTCAPS port_caps
;
244 IDirectMusicPort
* new_port
= NULL
;
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
)
256 return CLASS_E_NOAGGREGATION
;
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
);
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
);
281 This
->ports
= realloc(This
->ports
, sizeof(*This
->ports
) * This
->num_ports
);
282 This
->ports
[This
->num_ports
- 1] = new_port
;
288 return E_NOINTERFACE
;
291 void dmusic_remove_port(IDirectMusic8Impl
*dmusic
, IDirectMusicPort
*port
)
296 TRACE("Removing port %p.\n", port
);
298 for (i
= 0; i
< dmusic
->num_ports
; i
++)
300 if (dmusic
->ports
[i
] == port
) {
308 ERR("Port %p not found in ports array.\n", port
);
312 if (!--dmusic
->num_ports
) {
314 dmusic
->ports
= NULL
;
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
);
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");
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");
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
);
360 *guid_clock
= GUID_NULL
;
361 if (reference_clock
) {
362 *reference_clock
= This
->master_clock
;
363 IReferenceClock_AddRef(*reference_clock
);
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
));
378 static HRESULT WINAPI
IDirectMusic8Impl_Activate(LPDIRECTMUSIC8 iface
, BOOL enable
)
380 IDirectMusic8Impl
*This
= impl_from_IDirectMusic8(iface
);
384 TRACE("(%p)->(%u)\n", This
, enable
);
386 for (i
= 0; i
< This
->num_ports
; i
++)
388 hr
= IDirectMusicPort_Activate(This
->ports
[i
], enable
);
396 static HRESULT WINAPI
IDirectMusic8Impl_GetDefaultPort(LPDIRECTMUSIC8 iface
, LPGUID guid_port
)
398 IDirectMusic8Impl
*This
= impl_from_IDirectMusic8(iface
);
400 DWORD returnTypeGUID
, sizeOfReturnBuffer
= 50;
401 char returnBuffer
[51];
402 GUID defaultPortGUID
;
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
;
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
;
422 static HRESULT WINAPI
IDirectMusic8Impl_SetDirectSound(IDirectMusic8
*iface
, IDirectSound
*dsound
,
425 IDirectMusic8Impl
*This
= impl_from_IDirectMusic8(iface
);
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
);
439 IDirectSound_Release(This
->dsound
);
442 hr
= DirectSoundCreate8(NULL
, (IDirectSound8
**)&This
->dsound
, NULL
);
445 hr
= IDirectSound_SetCooperativeLevel(This
->dsound
, hwnd
? hwnd
: GetForegroundWindow(),
448 IDirectSound_Release(This
->dsound
);
454 IDirectSound_AddRef(dsound
);
455 This
->dsound
= dsound
;
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
);
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]";
492 MIDIOUTCAPSW caps_out
;
494 IDirectMusicSynth8
* synth
;
498 TRACE("(%p)\n", object
);
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
)
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
;
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
;
538 /* Fill midi out port info */
539 for (i
= 0; i
< nb_midi_out
; 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
;
551 /* Fill midi in port info */
552 for (i
= 0; i
< nb_midi_in
; 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
;
564 /* Fill synth port info */
565 port
->create
= synth_port_create
;
566 hr
= CoCreateInstance(&CLSID_DirectMusicSynth
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IDirectMusicSynth8
, (void**)&synth
);
569 port
->caps
.dwSize
= sizeof(port
->caps
);
570 hr
= IDirectMusicSynth8_GetPortCaps(synth
, &port
->caps
);
571 IDirectMusicSynth8_Release(synth
);
576 object
->num_system_ports
= nb_ports
;
579 HRESULT
music_create(IUnknown
**ret_iface
)
581 IDirectMusic8Impl
*dmusic
;
584 TRACE("(%p)\n", ret_iface
);
587 if (!(dmusic
= calloc(1, sizeof(*dmusic
)))) return E_OUTOFMEMORY
;
588 dmusic
->IDirectMusic8_iface
.lpVtbl
= &DirectMusic8_Vtbl
;
590 ret
= master_clock_create(&dmusic
->master_clock
);
596 create_system_ports_list(dmusic
);
598 TRACE("Created DirectMusic %p\n", dmusic
);
599 *ret_iface
= (IUnknown
*)&dmusic
->IDirectMusic8_iface
;