Fix some instances of memory allocation through HeapReAlloc().
[wine] / dlls / dmusic / dmusic.c
1 /* IDirectMusic8 Implementation
2  *
3  * Copyright (C) 2003 Rok Mandeljc
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <stdarg.h>
21
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winreg.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "winerror.h"
28 #include "mmsystem.h"
29 #include "winternl.h"
30 #include "mmddk.h"
31 #include "wine/windef16.h"
32 #include "wine/winbase16.h"
33 #include "wine/debug.h"
34 #include "dsound.h"
35
36 #include "dmusic_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
39
40 /* IDirectMusic8 IUnknown parts follow: */
41 HRESULT WINAPI IDirectMusic8Impl_QueryInterface (LPDIRECTMUSIC8 iface, REFIID riid, LPVOID *ppobj)
42 {
43         ICOM_THIS(IDirectMusic8Impl,iface);
44
45         if (IsEqualIID (riid, &IID_IUnknown) || 
46             IsEqualIID (riid, &IID_IDirectMusic2) ||
47             IsEqualIID (riid, &IID_IDirectMusic8)) {
48                 IDirectMusic8Impl_AddRef(iface);
49                 *ppobj = This;
50                 return S_OK;
51         }
52
53         WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
54         return E_NOINTERFACE;
55 }
56
57 ULONG WINAPI IDirectMusic8Impl_AddRef (LPDIRECTMUSIC8 iface)
58 {
59         ICOM_THIS(IDirectMusic8Impl,iface);
60         TRACE("(%p) : AddRef from %ld\n", This, This->ref);
61         return ++(This->ref);
62 }
63
64 ULONG WINAPI IDirectMusic8Impl_Release (LPDIRECTMUSIC8 iface)
65 {
66         ICOM_THIS(IDirectMusic8Impl,iface);
67         ULONG ref = --This->ref;
68         TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
69         if (ref == 0) {
70                 HeapFree(GetProcessHeap(), 0, This);
71         }
72         return ref;
73 }
74
75 /* IDirectMusic8 Interface follow: */
76 HRESULT WINAPI IDirectMusic8Impl_EnumPort(LPDIRECTMUSIC8 iface, DWORD dwIndex, LPDMUS_PORTCAPS pPortCaps)
77 {
78         ICOM_THIS(IDirectMusic8Impl,iface);
79         
80         TRACE("(%p, %ld, %p)\n", This, dwIndex, pPortCaps);
81         /* i guess the first port shown is always software synthesizer */
82         if (dwIndex == 0) 
83         {
84                 IDirectMusicSynth8* synth;
85                 TRACE("enumerating 'Microsoft Software Synthesizer' port\n");
86                 CoCreateInstance (&CLSID_DirectMusicSynth, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicSynth8, (void**)&synth);
87                 IDirectMusicSynth8_GetPortCaps (synth, pPortCaps);
88                 IDirectMusicSynth8_Release (synth);
89                 return S_OK;
90         }
91
92 /* it seems that the rest of devices are obtained thru dmusic32.EnumLegacyDevices...*sigh*...which is undocumented*/
93 #if 0
94         int numMIDI = midiOutGetNumDevs();
95         int numWAVE = waveOutGetNumDevs();
96         int i;
97         /* then return digital sound ports */
98         for (i = 1; i <= numWAVE; i++)
99         {
100                 TRACE("enumerating 'digital sound' ports\n");   
101                 if (i == dwIndex)
102                 {
103                         DirectSoundEnumerateA((LPDSENUMCALLBACKA) register_waveport, (VOID*) pPortCaps);
104                         return S_OK;    
105                 }
106         }
107         /* finally, list all *real* MIDI ports*/
108         for (i = numWAVE + 1; i <= numWAVE + numMIDI; i++) 
109         {
110                 TRACE("enumerating 'real MIDI' ports\n");               
111                 if (i == dwIndex)
112                         FIXME("Found MIDI port, but *real* MIDI ports not supported yet\n");
113         }
114 #endif  
115         return S_FALSE;
116 }
117
118 HRESULT WINAPI IDirectMusic8Impl_CreateMusicBuffer (LPDIRECTMUSIC8 iface, LPDMUS_BUFFERDESC pBufferDesc, LPDIRECTMUSICBUFFER** ppBuffer, LPUNKNOWN pUnkOuter)
119 {
120         ICOM_THIS(IDirectMusic8Impl,iface);
121
122         FIXME("(%p, %p, %p, %p): stub\n", This, pBufferDesc, ppBuffer, pUnkOuter);
123         
124         return S_OK;
125 }
126
127 HRESULT WINAPI IDirectMusic8Impl_CreatePort (LPDIRECTMUSIC8 iface, REFCLSID rclsidPort, LPDMUS_PORTPARAMS pPortParams, LPDIRECTMUSICPORT* ppPort, LPUNKNOWN pUnkOuter)
128 {
129         ICOM_THIS(IDirectMusic8Impl,iface);
130         int i/*, j*/;
131         DMUS_PORTCAPS PortCaps;
132         
133         TRACE("(%p, %s, %p, %p, %p)\n", This, debugstr_guid(rclsidPort), pPortParams, ppPort, pUnkOuter);
134         for (i = 0; S_FALSE != IDirectMusic8Impl_EnumPort(iface, i, &PortCaps); i++) {                          
135                 if (IsEqualCLSID (rclsidPort, &PortCaps.guidPort)) {            
136                         if(!This->ppPorts) This->ppPorts = HeapAlloc(GetProcessHeap(), 0, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
137                         else This->ppPorts = HeapReAlloc(GetProcessHeap(), 0, This->ppPorts, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
138                         if (NULL == This->ppPorts[This->nrofports]) {
139                                 *ppPort = (LPDIRECTMUSICPORT)NULL;
140                                 return E_OUTOFMEMORY;
141                         }
142                         This->ppPorts[This->nrofports]->lpVtbl = &DirectMusicPort_Vtbl;
143                         This->ppPorts[This->nrofports]->ref = 1;
144                         This->ppPorts[This->nrofports]->fActive = FALSE;
145                         This->ppPorts[This->nrofports]->pCaps = &PortCaps;
146                         This->ppPorts[This->nrofports]->pParams = pPortParams; /* this one is here just because there's a funct. which retrieves it back */
147                         This->ppPorts[This->nrofports]->pDirectSound = NULL;
148                         DMUSIC_CreateReferenceClock (&IID_IReferenceClock, (IReferenceClock**)&This->ppPorts[This->nrofports]->pLatencyClock, NULL);
149
150 #if 0
151                         if (pPortParams->dwValidParams & DMUS_PORTPARAMS_CHANNELGROUPS) {
152                                 This->ports[This->nrofports]->nrofgroups = pPortParams->dwChannelGroups;
153                                 /* setting default priorities */                        
154                                 for (j = 0; j < This->ports[This->nrofports]->nrofgroups; j++) {
155                                         TRACE ("Setting default channel priorities on channel group %i\n", j + 1);
156                                         This->ports[This->nrofports]->group[j].channel[0].priority = DAUD_CHAN1_DEF_VOICE_PRIORITY;
157                                         This->ports[This->nrofports]->group[j].channel[1].priority = DAUD_CHAN2_DEF_VOICE_PRIORITY;
158                                         This->ports[This->nrofports]->group[j].channel[2].priority = DAUD_CHAN3_DEF_VOICE_PRIORITY;
159                                         This->ports[This->nrofports]->group[j].channel[3].priority = DAUD_CHAN4_DEF_VOICE_PRIORITY;
160                                         This->ports[This->nrofports]->group[j].channel[4].priority = DAUD_CHAN5_DEF_VOICE_PRIORITY;
161                                         This->ports[This->nrofports]->group[j].channel[5].priority = DAUD_CHAN6_DEF_VOICE_PRIORITY;
162                                         This->ports[This->nrofports]->group[j].channel[6].priority = DAUD_CHAN7_DEF_VOICE_PRIORITY;
163                                         This->ports[This->nrofports]->group[j].channel[7].priority = DAUD_CHAN8_DEF_VOICE_PRIORITY;
164                                         This->ports[This->nrofports]->group[j].channel[8].priority = DAUD_CHAN9_DEF_VOICE_PRIORITY;
165                                         This->ports[This->nrofports]->group[j].channel[9].priority = DAUD_CHAN10_DEF_VOICE_PRIORITY;
166                                         This->ports[This->nrofports]->group[j].channel[10].priority = DAUD_CHAN11_DEF_VOICE_PRIORITY;
167                                         This->ports[This->nrofports]->group[j].channel[11].priority = DAUD_CHAN12_DEF_VOICE_PRIORITY;
168                                         This->ports[This->nrofports]->group[j].channel[12].priority = DAUD_CHAN13_DEF_VOICE_PRIORITY;
169                                         This->ports[This->nrofports]->group[j].channel[13].priority = DAUD_CHAN14_DEF_VOICE_PRIORITY;
170                                         This->ports[This->nrofports]->group[j].channel[14].priority = DAUD_CHAN15_DEF_VOICE_PRIORITY;
171                                         This->ports[This->nrofports]->group[j].channel[15].priority = DAUD_CHAN16_DEF_VOICE_PRIORITY;
172                                 }
173                         }
174 #endif
175                         *ppPort = (LPDIRECTMUSICPORT) This->ppPorts[This->nrofports];
176                         This->nrofports++;
177                         return S_OK;                    
178                 }
179         }
180         /* FIXME: place correct error here */
181         return E_NOINTERFACE;
182 }
183
184 HRESULT WINAPI IDirectMusic8Impl_EnumMasterClock (LPDIRECTMUSIC8 iface, DWORD dwIndex, LPDMUS_CLOCKINFO lpClockInfo)
185 {
186         ICOM_THIS(IDirectMusic8Impl,iface);
187
188         FIXME("(%p, %ld, %p): stub\n", This, dwIndex, lpClockInfo);
189
190         return S_FALSE;
191 }
192
193 HRESULT WINAPI IDirectMusic8Impl_GetMasterClock (LPDIRECTMUSIC8 iface, LPGUID pguidClock, IReferenceClock** ppReferenceClock)
194 {
195         ICOM_THIS(IDirectMusic8Impl,iface);
196
197         TRACE("(%p, %p, %p)\n", This, pguidClock, ppReferenceClock);
198         if (pguidClock)
199                 *pguidClock = This->pMasterClock->pClockInfo.guidClock;
200         if(ppReferenceClock)
201                 *ppReferenceClock = (IReferenceClock *)This->pMasterClock;
202
203         return S_OK;
204 }
205
206 HRESULT WINAPI IDirectMusic8Impl_SetMasterClock (LPDIRECTMUSIC8 iface, REFGUID rguidClock)
207 {
208         ICOM_THIS(IDirectMusic8Impl,iface);
209         
210         FIXME("(%p, %s): stub\n", This, debugstr_guid(rguidClock));
211         
212         return S_OK;
213 }
214
215 HRESULT WINAPI IDirectMusic8Impl_Activate (LPDIRECTMUSIC8 iface, BOOL fEnable)
216 {
217         ICOM_THIS(IDirectMusic8Impl,iface);
218         int i;
219         
220         FIXME("(%p, %d): stub\n", This, fEnable);
221         for (i = 0; i < This->nrofports; i++)   
222         {
223                 This->ppPorts[i]->fActive = fEnable;
224         }
225         
226         return S_OK;
227 }
228
229 HRESULT WINAPI IDirectMusic8Impl_GetDefaultPort (LPDIRECTMUSIC8 iface, LPGUID pguidPort)
230 {
231         ICOM_THIS(IDirectMusic8Impl,iface);
232         HKEY hkGUID;
233         DWORD returnTypeGUID, sizeOfReturnBuffer = 50;
234         char returnBuffer[51];
235         GUID defaultPortGUID;
236         WCHAR buff[51];
237
238         TRACE("(%p, %p)\n", This, pguidPort);
239         if ((RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic\\Defaults" , 0, KEY_READ, &hkGUID) != ERROR_SUCCESS) || 
240             (RegQueryValueExA(hkGUID, "DefaultOutputPort", NULL, &returnTypeGUID, returnBuffer, &sizeOfReturnBuffer) != ERROR_SUCCESS))
241         {
242                 WARN(": registry entry missing\n" );
243                 *pguidPort = CLSID_DirectMusicSynth;
244                 return S_OK;
245         }
246         /* FIXME: Check return types to ensure we're interpreting data right */
247         MultiByteToWideChar(CP_ACP, 0, returnBuffer, -1, buff, sizeof(buff) / sizeof(WCHAR));
248         CLSIDFromString((LPCOLESTR) buff, &defaultPortGUID);
249         *pguidPort = defaultPortGUID;
250         
251         return S_OK;
252 }
253
254 HRESULT WINAPI IDirectMusic8Impl_SetDirectSound (LPDIRECTMUSIC8 iface, LPDIRECTSOUND pDirectSound, HWND hWnd)
255 {
256         ICOM_THIS(IDirectMusic8Impl,iface);
257
258         FIXME("(%p, %p, %p): stub\n", This, pDirectSound, hWnd);
259
260         return S_OK;
261 }
262
263 HRESULT WINAPI IDirectMusic8Impl_SetExternalMasterClock (LPDIRECTMUSIC8 iface, IReferenceClock* pClock)
264 {
265         ICOM_THIS(IDirectMusic8Impl,iface);
266
267         FIXME("(%p, %p): stub\n", This, pClock);
268
269         return S_OK;
270 }
271
272 ICOM_VTABLE(IDirectMusic8) DirectMusic8_Vtbl =
273 {
274     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
275         IDirectMusic8Impl_QueryInterface,
276         IDirectMusic8Impl_AddRef,
277         IDirectMusic8Impl_Release,
278         IDirectMusic8Impl_EnumPort,
279         IDirectMusic8Impl_CreateMusicBuffer,
280         IDirectMusic8Impl_CreatePort,
281         IDirectMusic8Impl_EnumMasterClock,
282         IDirectMusic8Impl_GetMasterClock,
283         IDirectMusic8Impl_SetMasterClock,
284         IDirectMusic8Impl_Activate,
285         IDirectMusic8Impl_GetDefaultPort,
286         IDirectMusic8Impl_SetDirectSound,
287         IDirectMusic8Impl_SetExternalMasterClock
288 };
289
290 /* helper stuff */
291 void register_waveport (LPGUID lpGUID, LPCSTR lpszDesc, LPCSTR lpszDrvName, LPVOID lpContext)
292 {
293         LPDMUS_PORTCAPS pPortCaps = (LPDMUS_PORTCAPS)lpContext;
294         
295         pPortCaps->dwSize = sizeof(DMUS_PORTCAPS);
296         pPortCaps->dwFlags = DMUS_PC_DLS | DMUS_PC_SOFTWARESYNTH | DMUS_PC_DIRECTSOUND | DMUS_PC_DLS2 | DMUS_PC_AUDIOPATH | DMUS_PC_WAVE;
297         pPortCaps->guidPort = *lpGUID;
298         pPortCaps->dwClass = DMUS_PC_OUTPUTCLASS;
299         pPortCaps->dwType = DMUS_PORT_WINMM_DRIVER;
300         pPortCaps->dwMemorySize = DMUS_PC_SYSTEMMEMORY;
301         pPortCaps->dwMaxChannelGroups = 2;
302         pPortCaps->dwMaxVoices = -1;
303         pPortCaps->dwMaxAudioChannels = -1;
304         pPortCaps->dwEffectFlags = DMUS_EFFECT_REVERB | DMUS_EFFECT_CHORUS | DMUS_EFFECT_DELAY;
305         MultiByteToWideChar (CP_ACP, 0, lpszDesc, -1, pPortCaps->wszDescription, sizeof(pPortCaps->wszDescription)/sizeof(WCHAR));
306 }
307
308 /* for ClassFactory */
309 HRESULT WINAPI DMUSIC_CreateDirectMusic (LPCGUID lpcGUID, LPDIRECTMUSIC8 *ppDM, LPUNKNOWN pUnkOuter)
310 {
311         IDirectMusic8Impl *dmusic;
312
313         TRACE("(%p,%p,%p)\n",lpcGUID, ppDM, pUnkOuter);
314         if (IsEqualIID (lpcGUID, &IID_IDirectMusic) ||
315             IsEqualIID (lpcGUID, &IID_IDirectMusic2) ||
316             IsEqualIID (lpcGUID, &IID_IDirectMusic8)) {
317                 dmusic = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusic8Impl));
318                 if (NULL == dmusic) {
319                         *ppDM = (LPDIRECTMUSIC8) NULL;
320                         return E_OUTOFMEMORY;
321                 }
322                 dmusic->lpVtbl = &DirectMusic8_Vtbl;
323                 dmusic->ref = 1;
324                 dmusic->pMasterClock = NULL;
325                 dmusic->ppPorts = NULL;
326                 dmusic->nrofports = 0;
327                 DMUSIC_CreateReferenceClock (&IID_IReferenceClock, (IReferenceClock**)&dmusic->pMasterClock, NULL);
328                 
329                 *ppDM = (LPDIRECTMUSIC8) dmusic;
330                 return S_OK;
331         }
332         
333         WARN("No interface found\n");
334         return E_NOINTERFACE;
335 }