dbghelp: Prevent debug overflow of internal buffer while tracing.
[wine] / dlls / dmusic / dmusic.c
1 /* IDirectMusic8 Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19 #include "dmusic_private.h"
20
21 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
22
23 /* IDirectMusic8Impl IUnknown part: */
24 static HRESULT WINAPI IDirectMusic8Impl_QueryInterface (LPDIRECTMUSIC8 iface, REFIID riid, LPVOID *ppobj) {
25         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
26         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
27
28         if (IsEqualIID (riid, &IID_IUnknown) || 
29             IsEqualIID (riid, &IID_IDirectMusic) ||
30             IsEqualIID (riid, &IID_IDirectMusic2) ||
31             IsEqualIID (riid, &IID_IDirectMusic8)) {
32                 IUnknown_AddRef(iface);
33                 *ppobj = This;
34                 return S_OK;
35         }
36
37         WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
38         return E_NOINTERFACE;
39 }
40
41 static ULONG WINAPI IDirectMusic8Impl_AddRef (LPDIRECTMUSIC8 iface) {
42         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
43         ULONG refCount = InterlockedIncrement(&This->ref);
44
45         TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
46
47         DMUSIC_LockModule();
48
49         return refCount;
50 }
51
52 static ULONG WINAPI IDirectMusic8Impl_Release (LPDIRECTMUSIC8 iface) {
53         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
54         ULONG refCount = InterlockedDecrement(&This->ref);
55
56         TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
57
58         if (!refCount) {
59                 HeapFree(GetProcessHeap(), 0, This);
60         }
61
62         DMUSIC_UnlockModule();
63         
64         return refCount;
65 }
66
67 /* IDirectMusic8Impl IDirectMusic part: */
68 static HRESULT WINAPI IDirectMusic8Impl_EnumPort(LPDIRECTMUSIC8 iface, DWORD dwIndex, LPDMUS_PORTCAPS pPortCaps) {
69         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
70         TRACE("(%p, %d, %p)\n", This, dwIndex, pPortCaps);
71         if (NULL == pPortCaps) { return E_POINTER; }
72         /* i guess the first port shown is always software synthesizer */
73         if (dwIndex == 0) 
74         {
75                 IDirectMusicSynth8* synth;
76                 TRACE("enumerating 'Microsoft Software Synthesizer' port\n");
77                 CoCreateInstance (&CLSID_DirectMusicSynth, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicSynth8, (void**)&synth);
78                 IDirectMusicSynth8_GetPortCaps (synth, pPortCaps);
79                 IDirectMusicSynth8_Release (synth);
80                 return S_OK;
81         }
82
83 /* it seems that the rest of devices are obtained thru dmusic32.EnumLegacyDevices...*sigh*...which is undocumented*/
84 #if 0
85         int numMIDI = midiOutGetNumDevs();
86         int numWAVE = waveOutGetNumDevs();
87         int i;
88         /* then return digital sound ports */
89         for (i = 1; i <= numWAVE; i++)
90         {
91                 TRACE("enumerating 'digital sound' ports\n");   
92                 if (i == dwIndex)
93                 {
94                         DirectSoundEnumerateA((LPDSENUMCALLBACKA) register_waveport, (VOID*) pPortCaps);
95                         return S_OK;    
96                 }
97         }
98         /* finally, list all *real* MIDI ports*/
99         for (i = numWAVE + 1; i <= numWAVE + numMIDI; i++) 
100         {
101                 TRACE("enumerating 'real MIDI' ports\n");               
102                 if (i == dwIndex)
103                         FIXME("Found MIDI port, but *real* MIDI ports not supported yet\n");
104         }
105 #endif  
106         return S_FALSE;
107 }
108
109 static HRESULT WINAPI IDirectMusic8Impl_CreateMusicBuffer (LPDIRECTMUSIC8 iface, LPDMUS_BUFFERDESC pBufferDesc, LPDIRECTMUSICBUFFER** ppBuffer, LPUNKNOWN pUnkOuter) {
110         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
111         FIXME("(%p, %p, %p, %p): stub\n", This, pBufferDesc, ppBuffer, pUnkOuter);
112         return S_OK;
113 }
114
115 static HRESULT WINAPI IDirectMusic8Impl_CreatePort (LPDIRECTMUSIC8 iface, REFCLSID rclsidPort, LPDMUS_PORTPARAMS pPortParams, LPDIRECTMUSICPORT* ppPort, LPUNKNOWN pUnkOuter) {
116         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
117         int i/*, j*/;
118         DMUS_PORTCAPS PortCaps;
119         IDirectMusicPort* pNewPort = NULL;
120         HRESULT hr = E_FAIL;
121
122         TRACE("(%p, %s, %p, %p, %p)\n", This, debugstr_dmguid(rclsidPort), pPortParams, ppPort, pUnkOuter);     
123         ZeroMemory(&PortCaps, sizeof(DMUS_PORTCAPS));
124         PortCaps.dwSize = sizeof(DMUS_PORTCAPS);
125
126         for (i = 0; S_FALSE != IDirectMusic8Impl_EnumPort(iface, i, &PortCaps); i++) {                          
127                 if (IsEqualCLSID (rclsidPort, &PortCaps.guidPort)) {
128                         hr = DMUSIC_CreateDirectMusicPortImpl(&IID_IDirectMusicPort, (LPVOID*) &pNewPort, (LPUNKNOWN) This, pPortParams, &PortCaps);
129                         if (FAILED(hr)) {
130                           *ppPort = (LPDIRECTMUSICPORT) NULL;
131                           return hr;
132                         }
133                         This->nrofports++;
134                         if (!This->ppPorts) This->ppPorts = HeapAlloc(GetProcessHeap(), 0, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
135                         else This->ppPorts = HeapReAlloc(GetProcessHeap(), 0, This->ppPorts, sizeof(LPDIRECTMUSICPORT) * This->nrofports);                      
136                         This->ppPorts[This->nrofports - 1] = pNewPort;
137                         *ppPort = pNewPort;
138                         return S_OK;                    
139                 }
140         }
141         /* FIXME: place correct error here */
142         return E_NOINTERFACE;
143 }
144
145 static HRESULT WINAPI IDirectMusic8Impl_EnumMasterClock (LPDIRECTMUSIC8 iface, DWORD dwIndex, LPDMUS_CLOCKINFO lpClockInfo) {
146         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
147         FIXME("(%p, %d, %p): stub\n", This, dwIndex, lpClockInfo);
148         return S_FALSE;
149 }
150
151 static HRESULT WINAPI IDirectMusic8Impl_GetMasterClock (LPDIRECTMUSIC8 iface, LPGUID pguidClock, IReferenceClock** ppReferenceClock) {
152         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
153
154         TRACE("(%p, %p, %p)\n", This, pguidClock, ppReferenceClock);
155         if (pguidClock)
156                 *pguidClock = This->pMasterClock->pClockInfo.guidClock;
157         if(ppReferenceClock)
158                 *ppReferenceClock = (IReferenceClock *)This->pMasterClock;
159
160         return S_OK;
161 }
162
163 static HRESULT WINAPI IDirectMusic8Impl_SetMasterClock (LPDIRECTMUSIC8 iface, REFGUID rguidClock) {
164         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
165         FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidClock));
166         return S_OK;
167 }
168
169 static HRESULT WINAPI IDirectMusic8Impl_Activate (LPDIRECTMUSIC8 iface, BOOL fEnable) {
170         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
171         int i;
172         
173         FIXME("(%p, %d): stub\n", This, fEnable);
174         for (i = 0; i < This->nrofports; i++) {
175             IDirectMusicPortImpl_Activate(This->ppPorts[i], fEnable);
176         }
177         
178         return S_OK;
179 }
180
181 static HRESULT WINAPI IDirectMusic8Impl_GetDefaultPort (LPDIRECTMUSIC8 iface, LPGUID pguidPort) {
182         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
183         HKEY hkGUID;
184         DWORD returnTypeGUID, sizeOfReturnBuffer = 50;
185         char returnBuffer[51];
186         GUID defaultPortGUID;
187         WCHAR buff[51];
188
189         TRACE("(%p, %p)\n", This, pguidPort);
190         if ((RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic\\Defaults" , 0, KEY_READ, &hkGUID) != ERROR_SUCCESS) || 
191             (RegQueryValueExA(hkGUID, "DefaultOutputPort", NULL, &returnTypeGUID, (LPBYTE)returnBuffer, &sizeOfReturnBuffer) != ERROR_SUCCESS))
192         {
193                 WARN(": registry entry missing\n" );
194                 *pguidPort = CLSID_DirectMusicSynth;
195                 return S_OK;
196         }
197         /* FIXME: Check return types to ensure we're interpreting data right */
198         MultiByteToWideChar(CP_ACP, 0, returnBuffer, -1, buff, sizeof(buff) / sizeof(WCHAR));
199         CLSIDFromString(buff, &defaultPortGUID);
200         *pguidPort = defaultPortGUID;
201         
202         return S_OK;
203 }
204
205 static HRESULT WINAPI IDirectMusic8Impl_SetDirectSound (LPDIRECTMUSIC8 iface, LPDIRECTSOUND pDirectSound, HWND hWnd) {
206         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
207         FIXME("(%p, %p, %p): stub\n", This, pDirectSound, hWnd);
208         return S_OK;
209 }
210
211 static HRESULT WINAPI IDirectMusic8Impl_SetExternalMasterClock (LPDIRECTMUSIC8 iface, IReferenceClock* pClock) {
212         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
213         FIXME("(%p, %p): stub\n", This, pClock);
214         return S_OK;
215 }
216
217 static const IDirectMusic8Vtbl DirectMusic8_Vtbl = {
218         IDirectMusic8Impl_QueryInterface,
219         IDirectMusic8Impl_AddRef,
220         IDirectMusic8Impl_Release,
221         IDirectMusic8Impl_EnumPort,
222         IDirectMusic8Impl_CreateMusicBuffer,
223         IDirectMusic8Impl_CreatePort,
224         IDirectMusic8Impl_EnumMasterClock,
225         IDirectMusic8Impl_GetMasterClock,
226         IDirectMusic8Impl_SetMasterClock,
227         IDirectMusic8Impl_Activate,
228         IDirectMusic8Impl_GetDefaultPort,
229         IDirectMusic8Impl_SetDirectSound,
230         IDirectMusic8Impl_SetExternalMasterClock
231 };
232
233 /* helper stuff */
234 void register_waveport (LPGUID lpGUID, LPCSTR lpszDesc, LPCSTR lpszDrvName, LPVOID lpContext) {
235         LPDMUS_PORTCAPS pPortCaps = (LPDMUS_PORTCAPS)lpContext;
236         
237         pPortCaps->dwSize = sizeof(DMUS_PORTCAPS);
238         pPortCaps->dwFlags = DMUS_PC_DLS | DMUS_PC_SOFTWARESYNTH | DMUS_PC_DIRECTSOUND | DMUS_PC_DLS2 | DMUS_PC_AUDIOPATH | DMUS_PC_WAVE;
239         pPortCaps->guidPort = *lpGUID;
240         pPortCaps->dwClass = DMUS_PC_OUTPUTCLASS;
241         pPortCaps->dwType = DMUS_PORT_WINMM_DRIVER;
242         pPortCaps->dwMemorySize = DMUS_PC_SYSTEMMEMORY;
243         pPortCaps->dwMaxChannelGroups = 2;
244         pPortCaps->dwMaxVoices = -1;
245         pPortCaps->dwMaxAudioChannels = -1;
246         pPortCaps->dwEffectFlags = DMUS_EFFECT_REVERB | DMUS_EFFECT_CHORUS | DMUS_EFFECT_DELAY;
247         MultiByteToWideChar (CP_ACP, 0, lpszDesc, -1, pPortCaps->wszDescription, sizeof(pPortCaps->wszDescription)/sizeof(WCHAR));
248 }
249
250 /* for ClassFactory */
251 HRESULT WINAPI DMUSIC_CreateDirectMusicImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
252         IDirectMusic8Impl *dmusic;
253
254         TRACE("(%p,%p,%p)\n",lpcGUID, ppobj, pUnkOuter);
255
256         dmusic = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusic8Impl));
257         if (NULL == dmusic) {
258                 *ppobj = (LPDIRECTMUSIC8) NULL;
259                 return E_OUTOFMEMORY;
260         }
261         dmusic->lpVtbl = &DirectMusic8_Vtbl;
262         dmusic->ref = 0; /* will be inited with QueryInterface */
263         dmusic->pMasterClock = NULL;
264         dmusic->ppPorts = NULL;
265         dmusic->nrofports = 0;
266         DMUSIC_CreateReferenceClockImpl (&IID_IReferenceClock, (LPVOID*)&dmusic->pMasterClock, NULL);
267         
268         return IDirectMusic8Impl_QueryInterface ((LPDIRECTMUSIC8)dmusic, lpcGUID, ppobj);
269 }