dmusic: Enumerate ports for midi out and midi in devices in EnumPort.
[wine/multimedia.git] / dlls / dmusic / dmusic.c
blobb0dd7c98ac9e9b7c32d4953a147c9f0dab958418
1 /* IDirectMusic8 Implementation
3 * Copyright (C) 2003-2004 Rok Mandeljc
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.
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.
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
20 #include <stdio.h>
22 #include "dmusic_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
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);
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;
40 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
41 return E_NOINTERFACE;
44 static ULONG WINAPI IDirectMusic8Impl_AddRef (LPDIRECTMUSIC8 iface) {
45 IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
46 ULONG refCount = InterlockedIncrement(&This->ref);
48 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
50 DMUSIC_LockModule();
52 return refCount;
55 static ULONG WINAPI IDirectMusic8Impl_Release (LPDIRECTMUSIC8 iface) {
56 IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
57 ULONG refCount = InterlockedDecrement(&This->ref);
59 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
61 if (!refCount) {
62 HeapFree(GetProcessHeap(), 0, This->ppPorts);
63 HeapFree(GetProcessHeap(), 0, This);
66 DMUSIC_UnlockModule();
68 return refCount;
71 /* IDirectMusic8Impl IDirectMusic part: */
72 static HRESULT WINAPI IDirectMusic8Impl_EnumPort(LPDIRECTMUSIC8 iface, DWORD index, LPDMUS_PORTCAPS port_caps)
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};
79 TRACE("(%p, %d, %p)\n", This, index, port_caps);
81 if (!port_caps)
82 return E_POINTER;
84 /* NOTE: It seems some native versions get the rest of devices through dmusic32.EnumLegacyDevices...*sigh*...which is undocumented */
86 /* NOTE: Should we enum wave devices ? Native does not seem to */
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;
99 nb_midi_out = midiOutGetNumDevs();
101 if (index == 0)
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;
113 if (index < (nb_midi_out + 1))
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;
125 nb_midi_in = midiInGetNumDevs();
127 if (index < (nb_midi_in + nb_midi_out + 1))
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;
139 if (index == (nb_midi_in + nb_midi_out + 1))
141 IDirectMusicSynth8* synth;
142 TRACE("Enumerating port: 'Microsoft Software Synthesizer'\n");
143 CoCreateInstance(&CLSID_DirectMusicSynth, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicSynth8, (void**)&synth);
144 IDirectMusicSynth8_GetPortCaps(synth, port_caps);
145 IDirectMusicSynth8_Release(synth);
146 return S_OK;
149 return S_FALSE;
152 static HRESULT WINAPI IDirectMusic8Impl_CreateMusicBuffer (LPDIRECTMUSIC8 iface, LPDMUS_BUFFERDESC pBufferDesc, LPDIRECTMUSICBUFFER** ppBuffer, LPUNKNOWN pUnkOuter) {
153 IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
155 TRACE("(%p, %p, %p, %p)\n", This, pBufferDesc, ppBuffer, pUnkOuter);
157 if (pUnkOuter)
158 return CLASS_E_NOAGGREGATION;
160 if (!pBufferDesc || !ppBuffer)
161 return E_POINTER;
163 return DMUSIC_CreateDirectMusicBufferImpl(&IID_IDirectMusicBuffer, (LPVOID)ppBuffer, NULL);
166 static HRESULT WINAPI IDirectMusic8Impl_CreatePort (LPDIRECTMUSIC8 iface, REFCLSID rclsidPort, LPDMUS_PORTPARAMS pPortParams, LPDIRECTMUSICPORT* ppPort, LPUNKNOWN pUnkOuter) {
167 IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
168 int i;
169 DMUS_PORTCAPS PortCaps;
170 IDirectMusicPort* pNewPort = NULL;
171 HRESULT hr;
172 GUID default_port;
173 const GUID *request_port = rclsidPort;
175 TRACE("(%p, %s, %p, %p, %p)\n", This, debugstr_dmguid(rclsidPort), pPortParams, ppPort, pUnkOuter);
177 if(!rclsidPort)
178 return E_POINTER;
180 ZeroMemory(&PortCaps, sizeof(DMUS_PORTCAPS));
181 PortCaps.dwSize = sizeof(DMUS_PORTCAPS);
183 if(IsEqualGUID(request_port, &GUID_NULL)){
184 hr = IDirectMusic8_GetDefaultPort(iface, &default_port);
185 if(FAILED(hr))
186 return hr;
187 request_port = &default_port;
190 for (i = 0; S_FALSE != IDirectMusic8Impl_EnumPort(iface, i, &PortCaps); i++) {
191 if (IsEqualCLSID (request_port, &PortCaps.guidPort)) {
192 hr = DMUSIC_CreateDirectMusicPortImpl(&IID_IDirectMusicPort, (LPVOID*) &pNewPort, (LPUNKNOWN) This, pPortParams, &PortCaps);
193 if (FAILED(hr)) {
194 *ppPort = NULL;
195 return hr;
197 This->nrofports++;
198 if (!This->ppPorts) This->ppPorts = HeapAlloc(GetProcessHeap(), 0, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
199 else This->ppPorts = HeapReAlloc(GetProcessHeap(), 0, This->ppPorts, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
200 This->ppPorts[This->nrofports - 1] = pNewPort;
201 *ppPort = pNewPort;
202 return S_OK;
205 return E_NOINTERFACE;
208 static HRESULT WINAPI IDirectMusic8Impl_EnumMasterClock (LPDIRECTMUSIC8 iface, DWORD dwIndex, LPDMUS_CLOCKINFO lpClockInfo) {
209 IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
210 FIXME("(%p, %d, %p): stub\n", This, dwIndex, lpClockInfo);
211 return S_FALSE;
214 static HRESULT WINAPI IDirectMusic8Impl_GetMasterClock (LPDIRECTMUSIC8 iface, LPGUID pguidClock, IReferenceClock** ppReferenceClock) {
215 IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
217 TRACE("(%p, %p, %p)\n", This, pguidClock, ppReferenceClock);
218 if (pguidClock)
219 *pguidClock = This->pMasterClock->pClockInfo.guidClock;
220 if(ppReferenceClock)
221 *ppReferenceClock = (IReferenceClock *)This->pMasterClock;
223 return S_OK;
226 static HRESULT WINAPI IDirectMusic8Impl_SetMasterClock (LPDIRECTMUSIC8 iface, REFGUID rguidClock) {
227 IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
228 FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidClock));
229 return S_OK;
232 static HRESULT WINAPI IDirectMusic8Impl_Activate (LPDIRECTMUSIC8 iface, BOOL fEnable) {
233 IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
234 int i;
236 FIXME("(%p, %d): stub\n", This, fEnable);
237 for (i = 0; i < This->nrofports; i++) {
238 IDirectMusicPort_Activate(This->ppPorts[i], fEnable);
241 return S_OK;
244 static HRESULT WINAPI IDirectMusic8Impl_GetDefaultPort (LPDIRECTMUSIC8 iface, LPGUID pguidPort) {
245 IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
246 HKEY hkGUID;
247 DWORD returnTypeGUID, sizeOfReturnBuffer = 50;
248 char returnBuffer[51];
249 GUID defaultPortGUID;
250 WCHAR buff[51];
252 TRACE("(%p, %p)\n", This, pguidPort);
253 if ((RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic\\Defaults" , 0, KEY_READ, &hkGUID) != ERROR_SUCCESS) ||
254 (RegQueryValueExA(hkGUID, "DefaultOutputPort", NULL, &returnTypeGUID, (LPBYTE)returnBuffer, &sizeOfReturnBuffer) != ERROR_SUCCESS))
256 WARN(": registry entry missing\n" );
257 *pguidPort = CLSID_DirectMusicSynth;
258 return S_OK;
260 /* FIXME: Check return types to ensure we're interpreting data right */
261 MultiByteToWideChar(CP_ACP, 0, returnBuffer, -1, buff, sizeof(buff) / sizeof(WCHAR));
262 CLSIDFromString(buff, &defaultPortGUID);
263 *pguidPort = defaultPortGUID;
265 return S_OK;
268 static HRESULT WINAPI IDirectMusic8Impl_SetDirectSound (LPDIRECTMUSIC8 iface, LPDIRECTSOUND pDirectSound, HWND hWnd) {
269 IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
270 FIXME("(%p, %p, %p): stub\n", This, pDirectSound, hWnd);
271 return S_OK;
274 static HRESULT WINAPI IDirectMusic8Impl_SetExternalMasterClock (LPDIRECTMUSIC8 iface, IReferenceClock* pClock) {
275 IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
276 FIXME("(%p, %p): stub\n", This, pClock);
277 return S_OK;
280 static const IDirectMusic8Vtbl DirectMusic8_Vtbl = {
281 IDirectMusic8Impl_QueryInterface,
282 IDirectMusic8Impl_AddRef,
283 IDirectMusic8Impl_Release,
284 IDirectMusic8Impl_EnumPort,
285 IDirectMusic8Impl_CreateMusicBuffer,
286 IDirectMusic8Impl_CreatePort,
287 IDirectMusic8Impl_EnumMasterClock,
288 IDirectMusic8Impl_GetMasterClock,
289 IDirectMusic8Impl_SetMasterClock,
290 IDirectMusic8Impl_Activate,
291 IDirectMusic8Impl_GetDefaultPort,
292 IDirectMusic8Impl_SetDirectSound,
293 IDirectMusic8Impl_SetExternalMasterClock
296 /* for ClassFactory */
297 HRESULT WINAPI DMUSIC_CreateDirectMusicImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
298 IDirectMusic8Impl *dmusic;
300 TRACE("(%p,%p,%p)\n",lpcGUID, ppobj, pUnkOuter);
302 dmusic = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusic8Impl));
303 if (NULL == dmusic) {
304 *ppobj = NULL;
305 return E_OUTOFMEMORY;
307 dmusic->lpVtbl = &DirectMusic8_Vtbl;
308 dmusic->ref = 0; /* will be inited with QueryInterface */
309 dmusic->pMasterClock = NULL;
310 dmusic->ppPorts = NULL;
311 dmusic->nrofports = 0;
312 DMUSIC_CreateReferenceClockImpl (&IID_IReferenceClock, (LPVOID*)&dmusic->pMasterClock, NULL);
314 return IDirectMusic8Impl_QueryInterface ((LPDIRECTMUSIC8)dmusic, lpcGUID, ppobj);