* Sync up to trunk HEAD (r62975).
[reactos.git] / dll / directx / wine / dmusic / dmusic.c
1 /*
2 * IDirectMusic8 Implementation
3 *
4 * Copyright (C) 2003-2004 Rok Mandeljc
5 * Copyright (C) 2012 Christian Costa
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "dmusic_private.h"
23
24 #include <winreg.h>
25
26 static inline IDirectMusic8Impl *impl_from_IDirectMusic8(IDirectMusic8 *iface)
27 {
28 return CONTAINING_RECORD(iface, IDirectMusic8Impl, IDirectMusic8_iface);
29 }
30
31 /* IDirectMusic8Impl IUnknown part: */
32 static HRESULT WINAPI IDirectMusic8Impl_QueryInterface(LPDIRECTMUSIC8 iface, REFIID riid, LPVOID *ret_iface)
33 {
34 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
35
36 TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
37
38 if (IsEqualIID (riid, &IID_IUnknown) ||
39 IsEqualIID (riid, &IID_IDirectMusic) ||
40 IsEqualIID (riid, &IID_IDirectMusic2) ||
41 IsEqualIID (riid, &IID_IDirectMusic8))
42 {
43 IDirectMusic8_AddRef(iface);
44 *ret_iface = iface;
45 return S_OK;
46 }
47
48 *ret_iface = NULL;
49
50 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
51
52 return E_NOINTERFACE;
53 }
54
55 static ULONG WINAPI IDirectMusic8Impl_AddRef(LPDIRECTMUSIC8 iface)
56 {
57 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
58 ULONG ref = InterlockedIncrement(&This->ref);
59
60 TRACE("(%p)->(): new ref = %u\n", This, ref);
61
62 DMUSIC_LockModule();
63
64 return ref;
65 }
66
67 static ULONG WINAPI IDirectMusic8Impl_Release(LPDIRECTMUSIC8 iface)
68 {
69 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
70 ULONG ref = InterlockedDecrement(&This->ref);
71
72 TRACE("(%p)->(): new ref = %u\n", This, ref);
73
74 if (!ref) {
75 HeapFree(GetProcessHeap(), 0, This->system_ports);
76 HeapFree(GetProcessHeap(), 0, This->ppPorts);
77 HeapFree(GetProcessHeap(), 0, This);
78 }
79
80 DMUSIC_UnlockModule();
81
82 return ref;
83 }
84
85 /* IDirectMusic8Impl IDirectMusic part: */
86 static HRESULT WINAPI IDirectMusic8Impl_EnumPort(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_PORTCAPS port_caps)
87 {
88 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
89
90 TRACE("(%p, %d, %p)\n", This, index, port_caps);
91
92 if (!port_caps)
93 return E_POINTER;
94
95 if (index >= This->nb_system_ports)
96 return S_FALSE;
97
98 *port_caps = This->system_ports[index].caps;
99
100 return S_OK;
101 }
102
103 static HRESULT WINAPI IDirectMusic8Impl_CreateMusicBuffer(LPDIRECTMUSIC8 iface, LPDMUS_BUFFERDESC buffer_desc, LPDIRECTMUSICBUFFER* buffer, LPUNKNOWN unkouter)
104 {
105 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
106
107 TRACE("(%p)->(%p, %p, %p)\n", This, buffer_desc, buffer, unkouter);
108
109 if (unkouter)
110 return CLASS_E_NOAGGREGATION;
111
112 if (!buffer_desc || !buffer)
113 return E_POINTER;
114
115 return DMUSIC_CreateDirectMusicBufferImpl(buffer_desc, (LPVOID)buffer);
116 }
117
118 static HRESULT WINAPI IDirectMusic8Impl_CreatePort(LPDIRECTMUSIC8 iface, REFCLSID rclsid_port, LPDMUS_PORTPARAMS port_params, LPDIRECTMUSICPORT* port, LPUNKNOWN unkouter)
119 {
120 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
121 int i;
122 DMUS_PORTCAPS port_caps;
123 IDirectMusicPort* new_port = NULL;
124 HRESULT hr;
125 GUID default_port;
126 const GUID *request_port = rclsid_port;
127
128 TRACE("(%p)->(%s, %p, %p, %p)\n", This, debugstr_dmguid(rclsid_port), port_params, port, unkouter);
129
130 if (!rclsid_port)
131 return E_POINTER;
132 if (!port_params)
133 return E_INVALIDARG;
134 if (!port)
135 return E_POINTER;
136 if (unkouter)
137 return CLASS_E_NOAGGREGATION;
138
139 if (TRACE_ON(dmusic))
140 dump_DMUS_PORTPARAMS(port_params);
141
142 ZeroMemory(&port_caps, sizeof(DMUS_PORTCAPS));
143 port_caps.dwSize = sizeof(DMUS_PORTCAPS);
144
145 if (IsEqualGUID(request_port, &GUID_NULL)) {
146 hr = IDirectMusic8_GetDefaultPort(iface, &default_port);
147 if(FAILED(hr))
148 return hr;
149 request_port = &default_port;
150 }
151
152 for (i = 0; S_FALSE != IDirectMusic8Impl_EnumPort(iface, i, &port_caps); i++) {
153 if (IsEqualCLSID(request_port, &port_caps.guidPort)) {
154 hr = This->system_ports[i].create(&IID_IDirectMusicPort, (LPVOID*)&new_port, (LPUNKNOWN)This, port_params, &port_caps, This->system_ports[i].device);
155 if (FAILED(hr)) {
156 *port = NULL;
157 return hr;
158 }
159 This->nrofports++;
160 if (!This->ppPorts)
161 This->ppPorts = HeapAlloc(GetProcessHeap(), 0, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
162 else
163 This->ppPorts = HeapReAlloc(GetProcessHeap(), 0, This->ppPorts, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
164 This->ppPorts[This->nrofports - 1] = new_port;
165 *port = new_port;
166 return S_OK;
167 }
168 }
169
170 return E_NOINTERFACE;
171 }
172
173 static HRESULT WINAPI IDirectMusic8Impl_EnumMasterClock(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_CLOCKINFO clock_info)
174 {
175 TRACE("(%p)->(%d, %p)\n", iface, index, clock_info);
176
177 if (!clock_info)
178 return E_POINTER;
179
180 if (index > 1)
181 return S_FALSE;
182
183 if (!index)
184 {
185 static const GUID guid_system_clock = { 0x58d58419, 0x71b4, 0x11d1, { 0xa7, 0x4c, 0x00, 0x00, 0xf8, 0x75, 0xac, 0x12 } };
186 static const WCHAR name_system_clock[] = { 'S','y','s','t','e','m',' ','C','l','o','c','k',0 };
187
188 clock_info->ctType = 0;
189 clock_info->guidClock = guid_system_clock;
190 strcpyW(clock_info->wszDescription, name_system_clock);
191 }
192 else
193 {
194 static const GUID guid_dsound_clock = { 0x58d58420, 0x71b4, 0x11d1, { 0xa7, 0x4c, 0x00, 0x00, 0xf8, 0x75, 0xac, 0x12 } };
195 static const WCHAR name_dsound_clock[] = { 'D','i','r','e','c','t','S','o','u','n','d',' ','C','l','o','c','k',0 };
196
197 clock_info->ctType = 0;
198 clock_info->guidClock = guid_dsound_clock;
199 strcpyW(clock_info->wszDescription, name_dsound_clock);
200 }
201
202 return S_OK;
203 }
204
205 static HRESULT WINAPI IDirectMusic8Impl_GetMasterClock(LPDIRECTMUSIC8 iface, LPGUID guid_clock, IReferenceClock** reference_clock)
206 {
207 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
208
209 TRACE("(%p)->(%p, %p)\n", This, guid_clock, reference_clock);
210
211 if (guid_clock)
212 *guid_clock = This->pMasterClock->pClockInfo.guidClock;
213 if (reference_clock)
214 *reference_clock = (IReferenceClock*)This->pMasterClock;
215
216 return S_OK;
217 }
218
219 static HRESULT WINAPI IDirectMusic8Impl_SetMasterClock(LPDIRECTMUSIC8 iface, REFGUID rguidClock)
220 {
221 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
222
223 FIXME("(%p)->(%s): stub\n", This, debugstr_dmguid(rguidClock));
224
225 return S_OK;
226 }
227
228 static HRESULT WINAPI IDirectMusic8Impl_Activate(LPDIRECTMUSIC8 iface, BOOL enable)
229 {
230 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
231 int i;
232 HRESULT hr;
233
234 TRACE("(%p)->(%u)\n", This, enable);
235
236 for (i = 0; i < This->nrofports; i++)
237 {
238 hr = IDirectMusicPort_Activate(This->ppPorts[i], enable);
239 if (FAILED(hr))
240 return hr;
241 }
242
243 return S_OK;
244 }
245
246 static HRESULT WINAPI IDirectMusic8Impl_GetDefaultPort(LPDIRECTMUSIC8 iface, LPGUID guid_port)
247 {
248 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
249 HKEY hkGUID;
250 DWORD returnTypeGUID, sizeOfReturnBuffer = 50;
251 char returnBuffer[51];
252 GUID defaultPortGUID;
253 WCHAR buff[51];
254
255 TRACE("(%p)->(%p)\n", This, guid_port);
256
257 if ((RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic\\Defaults" , 0, KEY_READ, &hkGUID) != ERROR_SUCCESS) ||
258 (RegQueryValueExA(hkGUID, "DefaultOutputPort", NULL, &returnTypeGUID, (LPBYTE)returnBuffer, &sizeOfReturnBuffer) != ERROR_SUCCESS))
259 {
260 WARN(": registry entry missing\n" );
261 *guid_port = CLSID_DirectMusicSynth;
262 return S_OK;
263 }
264 /* FIXME: Check return types to ensure we're interpreting data right */
265 MultiByteToWideChar(CP_ACP, 0, returnBuffer, -1, buff, sizeof(buff) / sizeof(WCHAR));
266 CLSIDFromString(buff, &defaultPortGUID);
267 *guid_port = defaultPortGUID;
268
269 return S_OK;
270 }
271
272 static HRESULT WINAPI IDirectMusic8Impl_SetDirectSound(LPDIRECTMUSIC8 iface, LPDIRECTSOUND dsound, HWND wnd)
273 {
274 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
275
276 FIXME("(%p)->(%p, %p): stub\n", This, dsound, wnd);
277
278 return S_OK;
279 }
280
281 static HRESULT WINAPI IDirectMusic8Impl_SetExternalMasterClock(LPDIRECTMUSIC8 iface, IReferenceClock* clock)
282 {
283 IDirectMusic8Impl *This = impl_from_IDirectMusic8(iface);
284
285 FIXME("(%p)->(%p): stub\n", This, clock);
286
287 return S_OK;
288 }
289
290 static const IDirectMusic8Vtbl DirectMusic8_Vtbl = {
291 IDirectMusic8Impl_QueryInterface,
292 IDirectMusic8Impl_AddRef,
293 IDirectMusic8Impl_Release,
294 IDirectMusic8Impl_EnumPort,
295 IDirectMusic8Impl_CreateMusicBuffer,
296 IDirectMusic8Impl_CreatePort,
297 IDirectMusic8Impl_EnumMasterClock,
298 IDirectMusic8Impl_GetMasterClock,
299 IDirectMusic8Impl_SetMasterClock,
300 IDirectMusic8Impl_Activate,
301 IDirectMusic8Impl_GetDefaultPort,
302 IDirectMusic8Impl_SetDirectSound,
303 IDirectMusic8Impl_SetExternalMasterClock
304 };
305
306 static void create_system_ports_list(IDirectMusic8Impl* object)
307 {
308 port_info * port;
309 const WCHAR emulated[] = {' ','[','E','m','u','l','a','t','e','d',']',0};
310 ULONG nb_ports;
311 ULONG nb_midi_out;
312 ULONG nb_midi_in;
313 MIDIOUTCAPSW caps_out;
314 MIDIINCAPSW caps_in;
315 IDirectMusicSynth8* synth;
316 HRESULT hr;
317 ULONG i;
318
319 TRACE("(%p)\n", object);
320
321 /* NOTE:
322 - it seems some native versions get the rest of devices through dmusic32.EnumLegacyDevices...*sigh*...which is undocumented
323 - should we enum wave devices ? Native does not seem to
324 */
325
326 nb_midi_out = midiOutGetNumDevs();
327 nb_midi_in = midiInGetNumDevs();
328 nb_ports = 1 /* midi mapper */ + nb_midi_out + nb_midi_in + 1 /* synth port */;
329
330 port = object->system_ports = HeapAlloc(GetProcessHeap(), 0, nb_ports * sizeof(port_info));
331 if (!object->system_ports)
332 return;
333
334 /* Fill common port caps for all winmm ports */
335 for (i = 0; i < (nb_ports - 1 /* synth port*/); i++)
336 {
337 object->system_ports[i].caps.dwSize = sizeof(DMUS_PORTCAPS);
338 object->system_ports[i].caps.dwType = DMUS_PORT_WINMM_DRIVER;
339 object->system_ports[i].caps.dwMemorySize = 0;
340 object->system_ports[i].caps.dwMaxChannelGroups = 1;
341 object->system_ports[i].caps.dwMaxVoices = 0;
342 object->system_ports[i].caps.dwMaxAudioChannels = 0;
343 object->system_ports[i].caps.dwEffectFlags = DMUS_EFFECT_NONE;
344 /* Fake port GUID */
345 object->system_ports[i].caps.guidPort = IID_IUnknown;
346 object->system_ports[i].caps.guidPort.Data1 = i + 1;
347 }
348
349 /* Fill midi mapper port info */
350 port->device = MIDI_MAPPER;
351 port->create = DMUSIC_CreateMidiOutPortImpl;
352 midiOutGetDevCapsW(MIDI_MAPPER, &caps_out, sizeof(caps_out));
353 strcpyW(port->caps.wszDescription, caps_out.szPname);
354 strcatW(port->caps.wszDescription, emulated);
355 port->caps.dwFlags = DMUS_PC_SHAREABLE;
356 port->caps.dwClass = DMUS_PC_OUTPUTCLASS;
357 port++;
358
359 /* Fill midi out port info */
360 for (i = 0; i < nb_midi_out; i++)
361 {
362 port->device = i;
363 port->create = DMUSIC_CreateMidiOutPortImpl;
364 midiOutGetDevCapsW(i, &caps_out, sizeof(caps_out));
365 strcpyW(port->caps.wszDescription, caps_out.szPname);
366 strcatW(port->caps.wszDescription, emulated);
367 port->caps.dwFlags = DMUS_PC_SHAREABLE | DMUS_PC_EXTERNAL;
368 port->caps.dwClass = DMUS_PC_OUTPUTCLASS;
369 port++;
370 }
371
372 /* Fill midi in port info */
373 for (i = 0; i < nb_midi_in; i++)
374 {
375 port->device = i;
376 port->create = DMUSIC_CreateMidiInPortImpl;
377 midiInGetDevCapsW(i, &caps_in, sizeof(caps_in));
378 strcpyW(port->caps.wszDescription, caps_in.szPname);
379 strcatW(port->caps.wszDescription, emulated);
380 port->caps.dwFlags = DMUS_PC_EXTERNAL;
381 port->caps.dwClass = DMUS_PC_INPUTCLASS;
382 port++;
383 }
384
385 /* Fill synth port info */
386 port->create = DMUSIC_CreateSynthPortImpl;
387 hr = CoCreateInstance(&CLSID_DirectMusicSynth, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicSynth8, (void**)&synth);
388 if (SUCCEEDED(hr))
389 {
390 port->caps.dwSize = sizeof(port->caps);
391 hr = IDirectMusicSynth8_GetPortCaps(synth, &port->caps);
392 IDirectMusicSynth8_Release(synth);
393 }
394 if (FAILED(hr))
395 nb_ports--;
396
397 object->nb_system_ports = nb_ports;
398 }
399
400 /* For ClassFactory */
401 HRESULT WINAPI DMUSIC_CreateDirectMusicImpl(LPCGUID riid, LPVOID* ret_iface, LPUNKNOWN unkouter)
402 {
403 IDirectMusic8Impl *dmusic;
404 HRESULT ret;
405
406 TRACE("(%p,%p,%p)\n", riid, ret_iface, unkouter);
407
408 *ret_iface = NULL;
409 if (unkouter)
410 return CLASS_E_NOAGGREGATION;
411
412 dmusic = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusic8Impl));
413 if (!dmusic)
414 return E_OUTOFMEMORY;
415
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;
425 }
426
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;
432 }
433
434 create_system_ports_list(dmusic);
435
436 return S_OK;
437 }