jscript: Pass correct cpbegin to InitMatch.
[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->ppPorts);
60                 HeapFree(GetProcessHeap(), 0, This);
61         }
62
63         DMUSIC_UnlockModule();
64         
65         return refCount;
66 }
67
68 /* IDirectMusic8Impl IDirectMusic part: */
69 static HRESULT WINAPI IDirectMusic8Impl_EnumPort(LPDIRECTMUSIC8 iface, DWORD dwIndex, LPDMUS_PORTCAPS pPortCaps) {
70         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
71         TRACE("(%p, %d, %p)\n", This, dwIndex, pPortCaps);
72         if (NULL == pPortCaps) { return E_POINTER; }
73         /* i guess the first port shown is always software synthesizer */
74         if (dwIndex == 0) 
75         {
76                 IDirectMusicSynth8* synth;
77                 TRACE("enumerating 'Microsoft Software Synthesizer' port\n");
78                 CoCreateInstance (&CLSID_DirectMusicSynth, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicSynth8, (void**)&synth);
79                 IDirectMusicSynth8_GetPortCaps (synth, pPortCaps);
80                 IDirectMusicSynth8_Release (synth);
81                 return S_OK;
82         }
83
84 /* it seems that the rest of devices are obtained thru dmusic32.EnumLegacyDevices...*sigh*...which is undocumented*/
85 #if 0
86         int numMIDI = midiOutGetNumDevs();
87         int numWAVE = waveOutGetNumDevs();
88         int i;
89         /* then return digital sound ports */
90         for (i = 1; i <= numWAVE; i++)
91         {
92                 TRACE("enumerating 'digital sound' ports\n");   
93                 if (i == dwIndex)
94                 {
95                         DirectSoundEnumerateA(register_waveport, pPortCaps);
96                         return S_OK;    
97                 }
98         }
99         /* finally, list all *real* MIDI ports*/
100         for (i = numWAVE + 1; i <= numWAVE + numMIDI; i++) 
101         {
102                 TRACE("enumerating 'real MIDI' ports\n");               
103                 if (i == dwIndex)
104                         FIXME("Found MIDI port, but *real* MIDI ports not supported yet\n");
105         }
106 #endif  
107         return S_FALSE;
108 }
109
110 static HRESULT WINAPI IDirectMusic8Impl_CreateMusicBuffer (LPDIRECTMUSIC8 iface, LPDMUS_BUFFERDESC pBufferDesc, LPDIRECTMUSICBUFFER** ppBuffer, LPUNKNOWN pUnkOuter) {
111         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
112
113         TRACE("(%p, %p, %p, %p)\n", This, pBufferDesc, ppBuffer, pUnkOuter);
114
115         if (pUnkOuter)
116                 return CLASS_E_NOAGGREGATION;
117
118         if (!pBufferDesc || !ppBuffer)
119                 return E_POINTER;
120
121         return DMUSIC_CreateDirectMusicBufferImpl(&IID_IDirectMusicBuffer, (LPVOID)ppBuffer, NULL);
122 }
123
124 static HRESULT WINAPI IDirectMusic8Impl_CreatePort (LPDIRECTMUSIC8 iface, REFCLSID rclsidPort, LPDMUS_PORTPARAMS pPortParams, LPDIRECTMUSICPORT* ppPort, LPUNKNOWN pUnkOuter) {
125         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
126         int i/*, j*/;
127         DMUS_PORTCAPS PortCaps;
128         IDirectMusicPort* pNewPort = NULL;
129         HRESULT hr = E_FAIL;
130
131         TRACE("(%p, %s, %p, %p, %p)\n", This, debugstr_dmguid(rclsidPort), pPortParams, ppPort, pUnkOuter);     
132         ZeroMemory(&PortCaps, sizeof(DMUS_PORTCAPS));
133         PortCaps.dwSize = sizeof(DMUS_PORTCAPS);
134
135         for (i = 0; S_FALSE != IDirectMusic8Impl_EnumPort(iface, i, &PortCaps); i++) {                          
136                 if (IsEqualCLSID (rclsidPort, &PortCaps.guidPort)) {
137                         hr = DMUSIC_CreateDirectMusicPortImpl(&IID_IDirectMusicPort, (LPVOID*) &pNewPort, (LPUNKNOWN) This, pPortParams, &PortCaps);
138                         if (FAILED(hr)) {
139                           *ppPort = NULL;
140                           return hr;
141                         }
142                         This->nrofports++;
143                         if (!This->ppPorts) This->ppPorts = HeapAlloc(GetProcessHeap(), 0, sizeof(LPDIRECTMUSICPORT) * This->nrofports);
144                         else This->ppPorts = HeapReAlloc(GetProcessHeap(), 0, This->ppPorts, sizeof(LPDIRECTMUSICPORT) * This->nrofports);                      
145                         This->ppPorts[This->nrofports - 1] = pNewPort;
146                         *ppPort = pNewPort;
147                         return S_OK;                    
148                 }
149         }
150         /* FIXME: place correct error here */
151         return E_NOINTERFACE;
152 }
153
154 static HRESULT WINAPI IDirectMusic8Impl_EnumMasterClock (LPDIRECTMUSIC8 iface, DWORD dwIndex, LPDMUS_CLOCKINFO lpClockInfo) {
155         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
156         FIXME("(%p, %d, %p): stub\n", This, dwIndex, lpClockInfo);
157         return S_FALSE;
158 }
159
160 static HRESULT WINAPI IDirectMusic8Impl_GetMasterClock (LPDIRECTMUSIC8 iface, LPGUID pguidClock, IReferenceClock** ppReferenceClock) {
161         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
162
163         TRACE("(%p, %p, %p)\n", This, pguidClock, ppReferenceClock);
164         if (pguidClock)
165                 *pguidClock = This->pMasterClock->pClockInfo.guidClock;
166         if(ppReferenceClock)
167                 *ppReferenceClock = (IReferenceClock *)This->pMasterClock;
168
169         return S_OK;
170 }
171
172 static HRESULT WINAPI IDirectMusic8Impl_SetMasterClock (LPDIRECTMUSIC8 iface, REFGUID rguidClock) {
173         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
174         FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidClock));
175         return S_OK;
176 }
177
178 static HRESULT WINAPI IDirectMusic8Impl_Activate (LPDIRECTMUSIC8 iface, BOOL fEnable) {
179         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
180         int i;
181         
182         FIXME("(%p, %d): stub\n", This, fEnable);
183         for (i = 0; i < This->nrofports; i++) {
184             IDirectMusicPortImpl_Activate(This->ppPorts[i], fEnable);
185         }
186         
187         return S_OK;
188 }
189
190 static HRESULT WINAPI IDirectMusic8Impl_GetDefaultPort (LPDIRECTMUSIC8 iface, LPGUID pguidPort) {
191         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
192         HKEY hkGUID;
193         DWORD returnTypeGUID, sizeOfReturnBuffer = 50;
194         char returnBuffer[51];
195         GUID defaultPortGUID;
196         WCHAR buff[51];
197
198         TRACE("(%p, %p)\n", This, pguidPort);
199         if ((RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic\\Defaults" , 0, KEY_READ, &hkGUID) != ERROR_SUCCESS) || 
200             (RegQueryValueExA(hkGUID, "DefaultOutputPort", NULL, &returnTypeGUID, (LPBYTE)returnBuffer, &sizeOfReturnBuffer) != ERROR_SUCCESS))
201         {
202                 WARN(": registry entry missing\n" );
203                 *pguidPort = CLSID_DirectMusicSynth;
204                 return S_OK;
205         }
206         /* FIXME: Check return types to ensure we're interpreting data right */
207         MultiByteToWideChar(CP_ACP, 0, returnBuffer, -1, buff, sizeof(buff) / sizeof(WCHAR));
208         CLSIDFromString(buff, &defaultPortGUID);
209         *pguidPort = defaultPortGUID;
210         
211         return S_OK;
212 }
213
214 static HRESULT WINAPI IDirectMusic8Impl_SetDirectSound (LPDIRECTMUSIC8 iface, LPDIRECTSOUND pDirectSound, HWND hWnd) {
215         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
216         FIXME("(%p, %p, %p): stub\n", This, pDirectSound, hWnd);
217         return S_OK;
218 }
219
220 static HRESULT WINAPI IDirectMusic8Impl_SetExternalMasterClock (LPDIRECTMUSIC8 iface, IReferenceClock* pClock) {
221         IDirectMusic8Impl *This = (IDirectMusic8Impl *)iface;
222         FIXME("(%p, %p): stub\n", This, pClock);
223         return S_OK;
224 }
225
226 static const IDirectMusic8Vtbl DirectMusic8_Vtbl = {
227         IDirectMusic8Impl_QueryInterface,
228         IDirectMusic8Impl_AddRef,
229         IDirectMusic8Impl_Release,
230         IDirectMusic8Impl_EnumPort,
231         IDirectMusic8Impl_CreateMusicBuffer,
232         IDirectMusic8Impl_CreatePort,
233         IDirectMusic8Impl_EnumMasterClock,
234         IDirectMusic8Impl_GetMasterClock,
235         IDirectMusic8Impl_SetMasterClock,
236         IDirectMusic8Impl_Activate,
237         IDirectMusic8Impl_GetDefaultPort,
238         IDirectMusic8Impl_SetDirectSound,
239         IDirectMusic8Impl_SetExternalMasterClock
240 };
241
242 /* for ClassFactory */
243 HRESULT WINAPI DMUSIC_CreateDirectMusicImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
244         IDirectMusic8Impl *dmusic;
245
246         TRACE("(%p,%p,%p)\n",lpcGUID, ppobj, pUnkOuter);
247
248         dmusic = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusic8Impl));
249         if (NULL == dmusic) {
250                 *ppobj = NULL;
251                 return E_OUTOFMEMORY;
252         }
253         dmusic->lpVtbl = &DirectMusic8_Vtbl;
254         dmusic->ref = 0; /* will be inited with QueryInterface */
255         dmusic->pMasterClock = NULL;
256         dmusic->ppPorts = NULL;
257         dmusic->nrofports = 0;
258         DMUSIC_CreateReferenceClockImpl (&IID_IReferenceClock, (LPVOID*)&dmusic->pMasterClock, NULL);
259         
260         return IDirectMusic8Impl_QueryInterface ((LPDIRECTMUSIC8)dmusic, lpcGUID, ppobj);
261 }