dmusic: Check return codes and display name from port caps when enumerating microsoft...
[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
20 #include <stdio.h>
21
22 #include "dmusic_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
25
26 /* IDirectMusic8Impl IUnknown part: */
27 static HRESULT WINAPI IDirectMusic8Impl_QueryInterface (LPDIRECTMUSIC8 iface, REFIID riid, LPVOID *ppobj) {
28         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
29         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
30
31         if (IsEqualIID (riid, &IID_IUnknown) || 
32             IsEqualIID (riid, &IID_IDirectMusic) ||
33             IsEqualIID (riid, &IID_IDirectMusic2) ||
34             IsEqualIID (riid, &IID_IDirectMusic8)) {
35                 IUnknown_AddRef(iface);
36                 *ppobj = This;
37                 return S_OK;
38         }
39
40         WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
41         return E_NOINTERFACE;
42 }
43
44 static ULONG WINAPI IDirectMusic8Impl_AddRef (LPDIRECTMUSIC8 iface) {
45         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
46         ULONG refCount = InterlockedIncrement(&This->ref);
47
48         TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
49
50         DMUSIC_LockModule();
51
52         return refCount;
53 }
54
55 static ULONG WINAPI IDirectMusic8Impl_Release (LPDIRECTMUSIC8 iface) {
56         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
57         ULONG refCount = InterlockedDecrement(&This->ref);
58
59         TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
60
61         if (!refCount) {
62                 HeapFree(GetProcessHeap(), 0, This->ppPorts);
63                 HeapFree(GetProcessHeap(), 0, This);
64         }
65
66         DMUSIC_UnlockModule();
67         
68         return refCount;
69 }
70
71 /* IDirectMusic8Impl IDirectMusic part: */
72 static HRESULT WINAPI IDirectMusic8Impl_EnumPort(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_PORTCAPS port_caps)
73 {
74     IDirectMusic8Impl *This = (IDirectMusic8Impl*)iface;
75     ULONG nb_midi_out;
76     ULONG nb_midi_in;
77     const WCHAR emulated[] = {' ','[','E','m','u','l','a','t','e','d',']',0};
78
79     TRACE("(%p, %d, %p)\n", This, index, port_caps);
80
81     if (!port_caps)
82         return E_POINTER;
83
84     /* NOTE: It seems some native versions get the rest of devices through dmusic32.EnumLegacyDevices...*sigh*...which is undocumented */
85
86     /* NOTE: Should we enum wave devices ? Native does not seem to */
87
88     /* Fill common port caps for winmm ports */
89     port_caps->dwType = DMUS_PORT_WINMM_DRIVER;
90     port_caps->dwMemorySize = 0;
91     port_caps->dwMaxChannelGroups = 1;
92     port_caps->dwMaxVoices = 0;
93     port_caps->dwMaxAudioChannels = 0;
94     port_caps->dwEffectFlags = DMUS_EFFECT_NONE;
95     /* Fake port GUID */
96     port_caps->guidPort = IID_IUnknown;
97     port_caps->guidPort.Data1 = index + 1;
98
99     nb_midi_out = midiOutGetNumDevs();
100
101     if (index == 0)
102     {
103         MIDIOUTCAPSW caps;
104         midiOutGetDevCapsW(MIDI_MAPPER, &caps, sizeof(caps));
105         strcpyW(port_caps->wszDescription, caps.szPname);
106         strcatW(port_caps->wszDescription, emulated);
107         port_caps->dwFlags = DMUS_PC_SHAREABLE;
108         port_caps->dwClass = DMUS_PC_OUTPUTCLASS;
109         TRACE("Enumerating port: %s\n", debugstr_w(port_caps->wszDescription));
110         return S_OK;
111     }
112
113     if (index < (nb_midi_out + 1))
114     {
115         MIDIOUTCAPSW caps;
116         midiOutGetDevCapsW(index - 1, &caps, sizeof(caps));
117         strcpyW(port_caps->wszDescription, caps.szPname);
118         strcatW(port_caps->wszDescription, emulated);
119         port_caps->dwFlags = DMUS_PC_SHAREABLE | DMUS_PC_EXTERNAL;
120         port_caps->dwClass = DMUS_PC_OUTPUTCLASS;
121         TRACE("Enumerating port: %s\n", debugstr_w(port_caps->wszDescription));
122         return S_OK;
123     }
124
125     nb_midi_in = midiInGetNumDevs();
126
127     if (index < (nb_midi_in + nb_midi_out + 1))
128     {
129         MIDIINCAPSW caps;
130         midiInGetDevCapsW(index - nb_midi_out - 1, &caps, sizeof(caps));
131         strcpyW(port_caps->wszDescription, caps.szPname);
132         strcatW(port_caps->wszDescription, emulated);
133         port_caps->dwFlags = DMUS_PC_EXTERNAL;
134         port_caps->dwClass = DMUS_PC_INPUTCLASS;
135         TRACE("Enumerating port: %s\n", debugstr_w(port_caps->wszDescription));
136         return S_OK;
137     }
138
139     if (index == (nb_midi_in + nb_midi_out + 1))
140     {
141         IDirectMusicSynth8* synth = NULL;
142         HRESULT hr;
143         hr = CoCreateInstance(&CLSID_DirectMusicSynth, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicSynth8, (void**)&synth);
144         if (SUCCEEDED(hr))
145             IDirectMusicSynth8_GetPortCaps(synth, port_caps);
146         if (SUCCEEDED(hr))
147             TRACE("Enumerating port: %s\n", debugstr_w(port_caps->wszDescription));
148         if (synth)
149             IDirectMusicSynth8_Release(synth);
150         return hr;
151     }
152
153     return S_FALSE;
154 }
155
156 static HRESULT WINAPI IDirectMusic8Impl_CreateMusicBuffer (LPDIRECTMUSIC8 iface, LPDMUS_BUFFERDESC pBufferDesc, LPDIRECTMUSICBUFFER** ppBuffer, LPUNKNOWN pUnkOuter) {
157         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
158
159         TRACE("(%p, %p, %p, %p)\n", This, pBufferDesc, ppBuffer, pUnkOuter);
160
161         if (pUnkOuter)
162                 return CLASS_E_NOAGGREGATION;
163
164         if (!pBufferDesc || !ppBuffer)
165                 return E_POINTER;
166
167         return DMUSIC_CreateDirectMusicBufferImpl(&IID_IDirectMusicBuffer, (LPVOID)ppBuffer, NULL);
168 }
169
170 static HRESULT WINAPI IDirectMusic8Impl_CreatePort (LPDIRECTMUSIC8 iface, REFCLSID rclsidPort, LPDMUS_PORTPARAMS pPortParams, LPDIRECTMUSICPORT* ppPort, LPUNKNOWN pUnkOuter) {
171         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
172         int i;
173         DMUS_PORTCAPS PortCaps;
174         IDirectMusicPort* pNewPort = NULL;
175         HRESULT hr;
176         GUID default_port;
177         const GUID *request_port = rclsidPort;
178
179         TRACE("(%p, %s, %p, %p, %p)\n", This, debugstr_dmguid(rclsidPort), pPortParams, ppPort, pUnkOuter);
180
181         if(!rclsidPort)
182                 return E_POINTER;
183
184         ZeroMemory(&PortCaps, sizeof(DMUS_PORTCAPS));
185         PortCaps.dwSize = sizeof(DMUS_PORTCAPS);
186
187         if(IsEqualGUID(request_port, &GUID_NULL)){
188                 hr = IDirectMusic8_GetDefaultPort(iface, &default_port);
189                 if(FAILED(hr))
190                         return hr;
191                 request_port = &default_port;
192         }
193
194         for (i = 0; S_FALSE != IDirectMusic8Impl_EnumPort(iface, i, &PortCaps); i++) {
195                 if (IsEqualCLSID (request_port, &PortCaps.guidPort)) {
196                         hr = DMUSIC_CreateDirectMusicPortImpl(&IID_IDirectMusicPort, (LPVOID*) &pNewPort, (LPUNKNOWN) This, pPortParams, &PortCaps);
197                         if (FAILED(hr)) {
198                           *ppPort = NULL;
199                           return hr;
200                         }
201                         This->nrofports++;
202                         if (!This->ppPorts) This->ppPorts = HeapAlloc(GetProcessHeap(), 0, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
203                         else This->ppPorts = HeapReAlloc(GetProcessHeap(), 0, This->ppPorts, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
204                         This->ppPorts[This->nrofports - 1] = pNewPort;
205                         *ppPort = pNewPort;
206                         return S_OK;
207                 }
208         }
209         return E_NOINTERFACE;
210 }
211
212 static HRESULT WINAPI IDirectMusic8Impl_EnumMasterClock (LPDIRECTMUSIC8 iface, DWORD dwIndex, LPDMUS_CLOCKINFO lpClockInfo) {
213         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
214         FIXME("(%p, %d, %p): stub\n", This, dwIndex, lpClockInfo);
215         return S_FALSE;
216 }
217
218 static HRESULT WINAPI IDirectMusic8Impl_GetMasterClock (LPDIRECTMUSIC8 iface, LPGUID pguidClock, IReferenceClock** ppReferenceClock) {
219         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
220
221         TRACE("(%p, %p, %p)\n", This, pguidClock, ppReferenceClock);
222         if (pguidClock)
223                 *pguidClock = This->pMasterClock->pClockInfo.guidClock;
224         if(ppReferenceClock)
225                 *ppReferenceClock = (IReferenceClock *)This->pMasterClock;
226
227         return S_OK;
228 }
229
230 static HRESULT WINAPI IDirectMusic8Impl_SetMasterClock (LPDIRECTMUSIC8 iface, REFGUID rguidClock) {
231         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
232         FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidClock));
233         return S_OK;
234 }
235
236 static HRESULT WINAPI IDirectMusic8Impl_Activate (LPDIRECTMUSIC8 iface, BOOL fEnable) {
237         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
238         int i;
239         
240         FIXME("(%p, %d): stub\n", This, fEnable);
241         for (i = 0; i < This->nrofports; i++) {
242             IDirectMusicPort_Activate(This->ppPorts[i], fEnable);
243         }
244         
245         return S_OK;
246 }
247
248 static HRESULT WINAPI IDirectMusic8Impl_GetDefaultPort (LPDIRECTMUSIC8 iface, LPGUID pguidPort) {
249         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
250         HKEY hkGUID;
251         DWORD returnTypeGUID, sizeOfReturnBuffer = 50;
252         char returnBuffer[51];
253         GUID defaultPortGUID;
254         WCHAR buff[51];
255
256         TRACE("(%p, %p)\n", This, pguidPort);
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                 *pguidPort = 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         *pguidPort = defaultPortGUID;
268         
269         return S_OK;
270 }
271
272 static HRESULT WINAPI IDirectMusic8Impl_SetDirectSound (LPDIRECTMUSIC8 iface, LPDIRECTSOUND pDirectSound, HWND hWnd) {
273         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
274         FIXME("(%p, %p, %p): stub\n", This, pDirectSound, hWnd);
275         return S_OK;
276 }
277
278 static HRESULT WINAPI IDirectMusic8Impl_SetExternalMasterClock (LPDIRECTMUSIC8 iface, IReferenceClock* pClock) {
279         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
280         FIXME("(%p, %p): stub\n", This, pClock);
281         return S_OK;
282 }
283
284 static const IDirectMusic8Vtbl DirectMusic8_Vtbl = {
285         IDirectMusic8Impl_QueryInterface,
286         IDirectMusic8Impl_AddRef,
287         IDirectMusic8Impl_Release,
288         IDirectMusic8Impl_EnumPort,
289         IDirectMusic8Impl_CreateMusicBuffer,
290         IDirectMusic8Impl_CreatePort,
291         IDirectMusic8Impl_EnumMasterClock,
292         IDirectMusic8Impl_GetMasterClock,
293         IDirectMusic8Impl_SetMasterClock,
294         IDirectMusic8Impl_Activate,
295         IDirectMusic8Impl_GetDefaultPort,
296         IDirectMusic8Impl_SetDirectSound,
297         IDirectMusic8Impl_SetExternalMasterClock
298 };
299
300 /* for ClassFactory */
301 HRESULT WINAPI DMUSIC_CreateDirectMusicImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
302         IDirectMusic8Impl *dmusic;
303
304         TRACE("(%p,%p,%p)\n",lpcGUID, ppobj, pUnkOuter);
305
306         dmusic = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusic8Impl));
307         if (NULL == dmusic) {
308                 *ppobj = NULL;
309                 return E_OUTOFMEMORY;
310         }
311         dmusic->lpVtbl = &DirectMusic8_Vtbl;
312         dmusic->ref = 0; /* will be inited with QueryInterface */
313         dmusic->pMasterClock = NULL;
314         dmusic->ppPorts = NULL;
315         dmusic->nrofports = 0;
316         DMUSIC_CreateReferenceClockImpl (&IID_IReferenceClock, (LPVOID*)&dmusic->pMasterClock, NULL);
317         
318         return IDirectMusic8Impl_QueryInterface ((LPDIRECTMUSIC8)dmusic, lpcGUID, ppobj);
319 }