oleaut32: Added LoadTypeLib failure test.
[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(register_waveport, 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
112         TRACE("(%p, %p, %p, %p)\n", This, pBufferDesc, ppBuffer, pUnkOuter);
113
114         if (pUnkOuter)
115                 return CLASS_E_NOAGGREGATION;
116
117         if (!pBufferDesc || !ppBuffer)
118                 return E_POINTER;
119
120         return DMUSIC_CreateDirectMusicBufferImpl(&IID_IDirectMusicBuffer, (LPVOID)ppBuffer, NULL);
121 }
122
123 static HRESULT WINAPI IDirectMusic8Impl_CreatePort (LPDIRECTMUSIC8 iface, REFCLSID rclsidPort, LPDMUS_PORTPARAMS pPortParams, LPDIRECTMUSICPORT* ppPort, LPUNKNOWN pUnkOuter) {
124         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
125         int i/*, j*/;
126         DMUS_PORTCAPS PortCaps;
127         IDirectMusicPort* pNewPort = NULL;
128         HRESULT hr = E_FAIL;
129
130         TRACE("(%p, %s, %p, %p, %p)\n", This, debugstr_dmguid(rclsidPort), pPortParams, ppPort, pUnkOuter);     
131         ZeroMemory(&PortCaps, sizeof(DMUS_PORTCAPS));
132         PortCaps.dwSize = sizeof(DMUS_PORTCAPS);
133
134         for (i = 0; S_FALSE != IDirectMusic8Impl_EnumPort(iface, i, &PortCaps); i++) {                          
135                 if (IsEqualCLSID (rclsidPort, &PortCaps.guidPort)) {
136                         hr = DMUSIC_CreateDirectMusicPortImpl(&IID_IDirectMusicPort, (LPVOID*) &pNewPort, (LPUNKNOWN) This, pPortParams, &PortCaps);
137                         if (FAILED(hr)) {
138                           *ppPort = NULL;
139                           return hr;
140                         }
141                         This->nrofports++;
142                         if (!This->ppPorts) This->ppPorts = HeapAlloc(GetProcessHeap(), 0, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
143                         else This->ppPorts = HeapReAlloc(GetProcessHeap(), 0, This->ppPorts, sizeof(LPDIRECTMUSICPORT) * This->nrofports);                      
144                         This->ppPorts[This->nrofports - 1] = pNewPort;
145                         *ppPort = pNewPort;
146                         return S_OK;                    
147                 }
148         }
149         /* FIXME: place correct error here */
150         return E_NOINTERFACE;
151 }
152
153 static HRESULT WINAPI IDirectMusic8Impl_EnumMasterClock (LPDIRECTMUSIC8 iface, DWORD dwIndex, LPDMUS_CLOCKINFO lpClockInfo) {
154         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
155         FIXME("(%p, %d, %p): stub\n", This, dwIndex, lpClockInfo);
156         return S_FALSE;
157 }
158
159 static HRESULT WINAPI IDirectMusic8Impl_GetMasterClock (LPDIRECTMUSIC8 iface, LPGUID pguidClock, IReferenceClock** ppReferenceClock) {
160         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
161
162         TRACE("(%p, %p, %p)\n", This, pguidClock, ppReferenceClock);
163         if (pguidClock)
164                 *pguidClock = This->pMasterClock->pClockInfo.guidClock;
165         if(ppReferenceClock)
166                 *ppReferenceClock = (IReferenceClock *)This->pMasterClock;
167
168         return S_OK;
169 }
170
171 static HRESULT WINAPI IDirectMusic8Impl_SetMasterClock (LPDIRECTMUSIC8 iface, REFGUID rguidClock) {
172         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
173         FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidClock));
174         return S_OK;
175 }
176
177 static HRESULT WINAPI IDirectMusic8Impl_Activate (LPDIRECTMUSIC8 iface, BOOL fEnable) {
178         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
179         int i;
180         
181         FIXME("(%p, %d): stub\n", This, fEnable);
182         for (i = 0; i < This->nrofports; i++) {
183             IDirectMusicPortImpl_Activate(This->ppPorts[i], fEnable);
184         }
185         
186         return S_OK;
187 }
188
189 static HRESULT WINAPI IDirectMusic8Impl_GetDefaultPort (LPDIRECTMUSIC8 iface, LPGUID pguidPort) {
190         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
191         HKEY hkGUID;
192         DWORD returnTypeGUID, sizeOfReturnBuffer = 50;
193         char returnBuffer[51];
194         GUID defaultPortGUID;
195         WCHAR buff[51];
196
197         TRACE("(%p, %p)\n", This, pguidPort);
198         if ((RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic\\Defaults" , 0, KEY_READ, &hkGUID) != ERROR_SUCCESS) || 
199             (RegQueryValueExA(hkGUID, "DefaultOutputPort", NULL, &returnTypeGUID, (LPBYTE)returnBuffer, &sizeOfReturnBuffer) != ERROR_SUCCESS))
200         {
201                 WARN(": registry entry missing\n" );
202                 *pguidPort = CLSID_DirectMusicSynth;
203                 return S_OK;
204         }
205         /* FIXME: Check return types to ensure we're interpreting data right */
206         MultiByteToWideChar(CP_ACP, 0, returnBuffer, -1, buff, sizeof(buff) / sizeof(WCHAR));
207         CLSIDFromString(buff, &defaultPortGUID);
208         *pguidPort = defaultPortGUID;
209         
210         return S_OK;
211 }
212
213 static HRESULT WINAPI IDirectMusic8Impl_SetDirectSound (LPDIRECTMUSIC8 iface, LPDIRECTSOUND pDirectSound, HWND hWnd) {
214         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
215         FIXME("(%p, %p, %p): stub\n", This, pDirectSound, hWnd);
216         return S_OK;
217 }
218
219 static HRESULT WINAPI IDirectMusic8Impl_SetExternalMasterClock (LPDIRECTMUSIC8 iface, IReferenceClock* pClock) {
220         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
221         FIXME("(%p, %p): stub\n", This, pClock);
222         return S_OK;
223 }
224
225 static const IDirectMusic8Vtbl DirectMusic8_Vtbl = {
226         IDirectMusic8Impl_QueryInterface,
227         IDirectMusic8Impl_AddRef,
228         IDirectMusic8Impl_Release,
229         IDirectMusic8Impl_EnumPort,
230         IDirectMusic8Impl_CreateMusicBuffer,
231         IDirectMusic8Impl_CreatePort,
232         IDirectMusic8Impl_EnumMasterClock,
233         IDirectMusic8Impl_GetMasterClock,
234         IDirectMusic8Impl_SetMasterClock,
235         IDirectMusic8Impl_Activate,
236         IDirectMusic8Impl_GetDefaultPort,
237         IDirectMusic8Impl_SetDirectSound,
238         IDirectMusic8Impl_SetExternalMasterClock
239 };
240
241 /* for ClassFactory */
242 HRESULT WINAPI DMUSIC_CreateDirectMusicImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
243         IDirectMusic8Impl *dmusic;
244
245         TRACE("(%p,%p,%p)\n",lpcGUID, ppobj, pUnkOuter);
246
247         dmusic = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusic8Impl));
248         if (NULL == dmusic) {
249                 *ppobj = NULL;
250                 return E_OUTOFMEMORY;
251         }
252         dmusic->lpVtbl = &DirectMusic8_Vtbl;
253         dmusic->ref = 0; /* will be inited with QueryInterface */
254         dmusic->pMasterClock = NULL;
255         dmusic->ppPorts = NULL;
256         dmusic->nrofports = 0;
257         DMUSIC_CreateReferenceClockImpl (&IID_IReferenceClock, (LPVOID*)&dmusic->pMasterClock, NULL);
258         
259         return IDirectMusic8Impl_QueryInterface ((LPDIRECTMUSIC8)dmusic, lpcGUID, ppobj);
260 }