dmusic: COM cleanup of IDirectMusicInstrument and get rid of separated IUnknown inter...
[wine/multimedia.git] / dlls / dmusic / instrument.c
blob0578b7972f4bde77d057af0c112d33a74823d4be
1 /*
2 * IDirectMusicInstrument Implementation
4 * Copyright (C) 2003-2004 Rok Mandeljc
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "dmusic_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
25 static const GUID IID_IDirectMusicInstrumentPRIVATE = { 0xbcb20080, 0xa40c, 0x11d1, { 0x86, 0xbc, 0x00, 0xc0, 0x4f, 0xbf, 0x8f, 0xef } };
27 /* IDirectMusicInstrument IUnknown part: */
28 static HRESULT WINAPI IDirectMusicInstrumentImpl_QueryInterface(LPDIRECTMUSICINSTRUMENT iface, REFIID riid, LPVOID *ret_iface)
30 TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
32 if (IsEqualIID(riid, &IID_IUnknown) ||
33 IsEqualIID(riid, &IID_IDirectMusicInstrument))
35 *ret_iface = iface;
36 IDirectMusicInstrument_AddRef(iface);
37 return S_OK;
39 else if (IsEqualIID(riid, &IID_IDirectMusicInstrumentPRIVATE))
41 /* it seems to me that this interface is only basic IUnknown, without any
42 * other inherited functions... *sigh* this is the worst scenario, since it means
43 * that whoever calls it knows the layout of original implementation table and therefore
44 * tries to get data by direct access... expect crashes
46 FIXME("*sigh*... requested private/unspecified interface\n");
48 *ret_iface = iface;
49 IDirectMusicInstrument_AddRef(iface);
50 return S_OK;
53 WARN("(%p)->(%s, %p): not found\n", iface, debugstr_dmguid(riid), ret_iface);
55 return E_NOINTERFACE;
58 static ULONG WINAPI IDirectMusicInstrumentImpl_AddRef(LPDIRECTMUSICINSTRUMENT iface)
60 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
61 ULONG refCount = InterlockedIncrement(&This->ref);
63 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
65 DMUSIC_LockModule();
67 return refCount;
70 static ULONG WINAPI IDirectMusicInstrumentImpl_Release(LPDIRECTMUSICINSTRUMENT iface)
72 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
73 ULONG refCount = InterlockedDecrement(&This->ref);
75 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
77 if (!refCount) {
78 HeapFree(GetProcessHeap(), 0, This);
81 DMUSIC_UnlockModule();
83 return refCount;
86 /* IDirectMusicInstrumentImpl IDirectMusicInstrument part: */
87 static HRESULT WINAPI IDirectMusicInstrumentImpl_GetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD* pdwPatch)
89 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
91 TRACE("(%p)->(%p)\n", This, pdwPatch);
93 *pdwPatch = MIDILOCALE2Patch(&This->pHeader->Locale);
95 return S_OK;
98 static HRESULT WINAPI IDirectMusicInstrumentImpl_SetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD dwPatch)
100 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
102 TRACE("(%p)->(%d): stub\n", This, dwPatch);
104 Patch2MIDILOCALE(dwPatch, &This->pHeader->Locale);
106 return S_OK;
109 static const IDirectMusicInstrumentVtbl DirectMusicInstrument_Vtbl =
111 IDirectMusicInstrumentImpl_QueryInterface,
112 IDirectMusicInstrumentImpl_AddRef,
113 IDirectMusicInstrumentImpl_Release,
114 IDirectMusicInstrumentImpl_GetPatch,
115 IDirectMusicInstrumentImpl_SetPatch
118 /* for ClassFactory */
119 HRESULT DMUSIC_CreateDirectMusicInstrumentImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
120 IDirectMusicInstrumentImpl* dminst;
122 dminst = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicInstrumentImpl));
123 if (NULL == dminst) {
124 *ppobj = NULL;
125 return E_OUTOFMEMORY;
127 dminst->IDirectMusicInstrument_iface.lpVtbl = &DirectMusicInstrument_Vtbl;
128 dminst->ref = 0; /* will be inited by QueryInterface */
130 return IDirectMusicInstrument_QueryInterface(&dminst->IDirectMusicInstrument_iface, lpcGUID, ppobj);
133 static HRESULT read_from_stream(IStream *stream, void *data, ULONG size)
135 ULONG readed;
136 HRESULT hr;
138 hr = IStream_Read(stream, data, size, &readed);
139 if(FAILED(hr)){
140 TRACE("IStream_Read failed: %08x\n", hr);
141 return hr;
143 if(readed < size){
144 TRACE("Didn't read full chunk: %u < %u\n", readed, size);
145 return E_FAIL;
148 return S_OK;
151 static inline DWORD subtract_bytes(DWORD len, DWORD bytes)
153 if(bytes > len){
154 TRACE("Apparent mismatch in chunk lengths? %u bytes remaining, %u bytes read\n", len, bytes);
155 return 0;
157 return len - bytes;
160 static HRESULT load_instrument(IDirectMusicInstrumentImpl *This, IStream *stream, DWORD length)
162 HRESULT hr;
163 FOURCC fourcc;
164 DWORD bytes;
165 LARGE_INTEGER move;
167 while(length){
168 hr = read_from_stream(stream, &fourcc, sizeof(fourcc));
169 if(FAILED(hr))
170 return hr;
172 hr = read_from_stream(stream, &bytes, sizeof(bytes));
173 if(FAILED(hr))
174 return hr;
176 length = subtract_bytes(length, sizeof(fourcc) + sizeof(bytes));
178 switch(fourcc){
179 case FOURCC_INSH:
180 TRACE("INSH chunk: %u bytes\n", bytes);
181 hr = read_from_stream(stream, This->pHeader, sizeof(*This->pHeader));
182 if(FAILED(hr))
183 return hr;
185 move.QuadPart = bytes - sizeof(*This->pHeader);
186 hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
187 if(FAILED(hr)){
188 WARN("IStream_Seek failed: %08x\n", hr);
189 return hr;
192 length = subtract_bytes(length, bytes);
193 break;
195 case FOURCC_DLID:
196 TRACE("DLID chunk: %u bytes\n", bytes);
197 hr = read_from_stream(stream, This->pInstrumentID, sizeof(*This->pInstrumentID));
198 if(FAILED(hr))
199 return hr;
201 move.QuadPart = bytes - sizeof(*This->pInstrumentID);
202 hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
203 if(FAILED(hr)){
204 WARN("IStream_Seek failed: %08x\n", hr);
205 return hr;
208 length = subtract_bytes(length, bytes);
209 break;
211 default:
212 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(fourcc), bytes);
214 move.QuadPart = bytes;
215 hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
216 if(FAILED(hr)){
217 WARN("IStream_Seek failed: %08x\n", hr);
218 return hr;
221 length = subtract_bytes(length, bytes);
222 break;
226 return S_OK;
229 /* aux. function that completely loads instrument; my tests indicate that it's
230 called somewhere around IDirectMusicCollection_GetInstrument */
231 HRESULT IDirectMusicInstrumentImpl_Custom_Load(LPDIRECTMUSICINSTRUMENT iface, LPSTREAM stream)
233 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
234 LARGE_INTEGER move;
235 FOURCC fourcc;
236 DWORD bytes;
237 HRESULT hr;
239 TRACE("(%p, %p, offset = %s)\n", This, stream, wine_dbgstr_longlong(This->liInstrumentPosition.QuadPart));
241 hr = IStream_Seek(stream, This->liInstrumentPosition, STREAM_SEEK_SET, NULL);
242 if(FAILED(hr)){
243 WARN("IStream_Seek failed: %08x\n", hr);
244 goto load_failure;
247 hr = read_from_stream(stream, &fourcc, sizeof(fourcc));
248 if(FAILED(hr))
249 goto load_failure;
251 if(fourcc != FOURCC_LIST){
252 WARN("Loading failed: Expected LIST chunk, got: %s\n", debugstr_fourcc(fourcc));
253 goto load_failure;
256 hr = read_from_stream(stream, &bytes, sizeof(bytes));
257 if(FAILED(hr))
258 goto load_failure;
260 TRACE("LIST chunk: %u bytes\n", bytes);
261 while(1){
262 hr = read_from_stream(stream, &fourcc, sizeof(fourcc));
263 if(FAILED(hr))
264 goto load_failure;
266 switch(fourcc){
267 case FOURCC_INS:
268 TRACE("INS chunk: (no byte count)\n");
269 hr = load_instrument(This, stream, bytes - sizeof(FOURCC));
270 if(FAILED(hr))
271 goto load_failure;
272 break;
274 default:
275 hr = read_from_stream(stream, &bytes, sizeof(bytes));
276 if(FAILED(hr))
277 goto load_failure;
279 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(fourcc), bytes);
281 move.QuadPart = bytes;
282 hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
283 if(FAILED(hr)){
284 WARN("IStream_Seek failed: %08x\n", hr);
285 return hr;
288 break;
292 return S_OK;
294 load_failure:
295 return DMUS_E_UNSUPPORTED_STREAM;