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