propsys: Implement prop variant integer conversions with tests.
[wine] / dlls / dmusic / instrument.c
1 /*
2  * IDirectMusicInstrument Implementation
3  *
4  * Copyright (C) 2003-2004 Rok Mandeljc
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "dmusic_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
24
25 static const GUID IID_IDirectMusicInstrumentPRIVATE = { 0xbcb20080, 0xa40c, 0x11d1, { 0x86, 0xbc, 0x00, 0xc0, 0x4f, 0xbf, 0x8f, 0xef } };
26
27 /* IDirectMusicInstrument IUnknown part: */
28 static HRESULT WINAPI IDirectMusicInstrumentImpl_QueryInterface(LPDIRECTMUSICINSTRUMENT iface, REFIID riid, LPVOID *ret_iface)
29 {
30     TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
31
32     if (IsEqualIID(riid, &IID_IUnknown) ||
33         IsEqualIID(riid, &IID_IDirectMusicInstrument))
34     {
35         *ret_iface = iface;
36         IDirectMusicInstrument_AddRef(iface);
37         return S_OK;
38     }
39     else if (IsEqualIID(riid, &IID_IDirectMusicInstrumentPRIVATE))
40     {
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
45          */
46         FIXME("*sigh*... requested private/unspecified interface\n");
47
48         *ret_iface = iface;
49         IDirectMusicInstrument_AddRef(iface);
50         return S_OK;
51     }
52
53     WARN("(%p)->(%s, %p): not found\n", iface, debugstr_dmguid(riid), ret_iface);
54
55     return E_NOINTERFACE;
56 }
57
58 static ULONG WINAPI IDirectMusicInstrumentImpl_AddRef(LPDIRECTMUSICINSTRUMENT iface)
59 {
60     IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
61     ULONG ref = InterlockedIncrement(&This->ref);
62
63     TRACE("(%p)->(): new ref = %u\n", iface, ref);
64
65     DMUSIC_LockModule();
66
67     return ref;
68 }
69
70 static ULONG WINAPI IDirectMusicInstrumentImpl_Release(LPDIRECTMUSICINSTRUMENT iface)
71 {
72     IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
73     ULONG ref = InterlockedDecrement(&This->ref);
74
75     TRACE("(%p)->(): new ref = %u\n", iface, ref);
76
77     if (!ref)
78         HeapFree(GetProcessHeap(), 0, This);
79
80     DMUSIC_UnlockModule();
81
82     return ref;
83 }
84
85 /* IDirectMusicInstrumentImpl IDirectMusicInstrument part: */
86 static HRESULT WINAPI IDirectMusicInstrumentImpl_GetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD* pdwPatch)
87 {
88     IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
89
90     TRACE("(%p)->(%p)\n", This, pdwPatch);
91
92     *pdwPatch = MIDILOCALE2Patch(&This->pHeader->Locale);
93
94     return S_OK;
95 }
96
97 static HRESULT WINAPI IDirectMusicInstrumentImpl_SetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD dwPatch)
98 {
99     IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
100
101     TRACE("(%p)->(%d): stub\n", This, dwPatch);
102
103     Patch2MIDILOCALE(dwPatch, &This->pHeader->Locale);
104
105     return S_OK;
106 }
107
108 static const IDirectMusicInstrumentVtbl DirectMusicInstrument_Vtbl =
109 {
110     IDirectMusicInstrumentImpl_QueryInterface,
111     IDirectMusicInstrumentImpl_AddRef,
112     IDirectMusicInstrumentImpl_Release,
113     IDirectMusicInstrumentImpl_GetPatch,
114     IDirectMusicInstrumentImpl_SetPatch
115 };
116
117 /* for ClassFactory */
118 HRESULT DMUSIC_CreateDirectMusicInstrumentImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
119         IDirectMusicInstrumentImpl* dminst;
120         
121         dminst = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicInstrumentImpl));
122         if (NULL == dminst) {
123                 *ppobj = NULL;
124                 return E_OUTOFMEMORY;
125         }
126         dminst->IDirectMusicInstrument_iface.lpVtbl = &DirectMusicInstrument_Vtbl;
127         dminst->ref = 0; /* will be inited by QueryInterface */
128         
129         return IDirectMusicInstrument_QueryInterface(&dminst->IDirectMusicInstrument_iface, lpcGUID, ppobj);
130 }
131
132 static HRESULT read_from_stream(IStream *stream, void *data, ULONG size)
133 {
134     ULONG bytes_read;
135     HRESULT hr;
136
137     hr = IStream_Read(stream, data, size, &bytes_read);
138     if(FAILED(hr)){
139         TRACE("IStream_Read failed: %08x\n", hr);
140         return hr;
141     }
142     if (bytes_read < size) {
143         TRACE("Didn't read full chunk: %u < %u\n", bytes_read, size);
144         return E_FAIL;
145     }
146
147     return S_OK;
148 }
149
150 static inline DWORD subtract_bytes(DWORD len, DWORD bytes)
151 {
152     if(bytes > len){
153         TRACE("Apparent mismatch in chunk lengths? %u bytes remaining, %u bytes read\n", len, bytes);
154         return 0;
155     }
156     return len - bytes;
157 }
158
159 static HRESULT load_instrument(IDirectMusicInstrumentImpl *This, IStream *stream, DWORD length)
160 {
161     HRESULT hr;
162     FOURCC fourcc;
163     DWORD bytes;
164     LARGE_INTEGER move;
165
166     while(length){
167         hr = read_from_stream(stream, &fourcc, sizeof(fourcc));
168         if(FAILED(hr))
169             return hr;
170
171         hr = read_from_stream(stream, &bytes, sizeof(bytes));
172         if(FAILED(hr))
173             return hr;
174
175         length = subtract_bytes(length, sizeof(fourcc) + sizeof(bytes));
176
177         switch(fourcc){
178         case FOURCC_INSH:
179             TRACE("INSH chunk: %u bytes\n", bytes);
180             hr = read_from_stream(stream, This->pHeader, sizeof(*This->pHeader));
181             if(FAILED(hr))
182                 return hr;
183
184             move.QuadPart = bytes - sizeof(*This->pHeader);
185             hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
186             if(FAILED(hr)){
187                 WARN("IStream_Seek failed: %08x\n", hr);
188                 return hr;
189             }
190
191             length = subtract_bytes(length, bytes);
192             break;
193
194         case FOURCC_DLID:
195             TRACE("DLID chunk: %u bytes\n", bytes);
196             hr = read_from_stream(stream, This->pInstrumentID, sizeof(*This->pInstrumentID));
197             if(FAILED(hr))
198                 return hr;
199
200             move.QuadPart = bytes - sizeof(*This->pInstrumentID);
201             hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
202             if(FAILED(hr)){
203                 WARN("IStream_Seek failed: %08x\n", hr);
204                 return hr;
205             }
206
207             length = subtract_bytes(length, bytes);
208             break;
209
210         default:
211             TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(fourcc), bytes);
212
213             move.QuadPart = bytes;
214             hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
215             if(FAILED(hr)){
216                 WARN("IStream_Seek failed: %08x\n", hr);
217                 return hr;
218             }
219
220             length = subtract_bytes(length, bytes);
221             break;
222         }
223     }
224
225     return S_OK;
226 }
227
228 /* aux. function that completely loads instrument; my tests indicate that it's 
229    called somewhere around IDirectMusicCollection_GetInstrument */
230 HRESULT IDirectMusicInstrumentImpl_Custom_Load(LPDIRECTMUSICINSTRUMENT iface, LPSTREAM stream)
231 {
232     IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
233     LARGE_INTEGER move;
234     FOURCC fourcc;
235     DWORD bytes;
236     HRESULT hr;
237
238     TRACE("(%p, %p, offset = %s)\n", This, stream, wine_dbgstr_longlong(This->liInstrumentPosition.QuadPart));
239
240     hr = IStream_Seek(stream, This->liInstrumentPosition, STREAM_SEEK_SET, NULL);
241     if(FAILED(hr)){
242         WARN("IStream_Seek failed: %08x\n", hr);
243         goto load_failure;
244     }
245
246     hr = read_from_stream(stream, &fourcc, sizeof(fourcc));
247     if(FAILED(hr))
248         goto load_failure;
249
250     if(fourcc != FOURCC_LIST){
251         WARN("Loading failed: Expected LIST chunk, got: %s\n", debugstr_fourcc(fourcc));
252         goto load_failure;
253     }
254
255     hr = read_from_stream(stream, &bytes, sizeof(bytes));
256     if(FAILED(hr))
257         goto load_failure;
258
259     TRACE("LIST chunk: %u bytes\n", bytes);
260     while(1){
261         hr = read_from_stream(stream, &fourcc, sizeof(fourcc));
262         if(FAILED(hr))
263             goto load_failure;
264
265         switch(fourcc){
266         case FOURCC_INS:
267             TRACE("INS  chunk: (no byte count)\n");
268             hr = load_instrument(This, stream, bytes - sizeof(FOURCC));
269             if(FAILED(hr))
270                 goto load_failure;
271             break;
272
273         default:
274             hr = read_from_stream(stream, &bytes, sizeof(bytes));
275             if(FAILED(hr))
276                 goto load_failure;
277
278             TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(fourcc), bytes);
279
280             move.QuadPart = bytes;
281             hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
282             if(FAILED(hr)){
283                 WARN("IStream_Seek failed: %08x\n", hr);
284                 return hr;
285             }
286
287             break;
288         }
289     }
290
291     return S_OK;
292
293 load_failure:
294     return DMUS_E_UNSUPPORTED_STREAM;
295 }