usp10: Don't compute a pair adjustment for the last glyph.
[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     {
79         HeapFree(GetProcessHeap(), 0, This->regions);
80         HeapFree(GetProcessHeap(), 0, This);
81     }
82
83     DMUSIC_UnlockModule();
84
85     return ref;
86 }
87
88 /* IDirectMusicInstrumentImpl IDirectMusicInstrument part: */
89 static HRESULT WINAPI IDirectMusicInstrumentImpl_GetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD* pdwPatch)
90 {
91     IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
92
93     TRACE("(%p)->(%p)\n", This, pdwPatch);
94
95     *pdwPatch = MIDILOCALE2Patch(&This->header.Locale);
96
97     return S_OK;
98 }
99
100 static HRESULT WINAPI IDirectMusicInstrumentImpl_SetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD dwPatch)
101 {
102     IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
103
104     TRACE("(%p)->(%d): stub\n", This, dwPatch);
105
106     Patch2MIDILOCALE(dwPatch, &This->header.Locale);
107
108     return S_OK;
109 }
110
111 static const IDirectMusicInstrumentVtbl DirectMusicInstrument_Vtbl =
112 {
113     IDirectMusicInstrumentImpl_QueryInterface,
114     IDirectMusicInstrumentImpl_AddRef,
115     IDirectMusicInstrumentImpl_Release,
116     IDirectMusicInstrumentImpl_GetPatch,
117     IDirectMusicInstrumentImpl_SetPatch
118 };
119
120 /* for ClassFactory */
121 HRESULT DMUSIC_CreateDirectMusicInstrumentImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
122         IDirectMusicInstrumentImpl* dminst;
123         
124         dminst = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicInstrumentImpl));
125         if (NULL == dminst) {
126                 *ppobj = NULL;
127                 return E_OUTOFMEMORY;
128         }
129         dminst->IDirectMusicInstrument_iface.lpVtbl = &DirectMusicInstrument_Vtbl;
130         dminst->ref = 0; /* will be inited by QueryInterface */
131         
132         return IDirectMusicInstrument_QueryInterface(&dminst->IDirectMusicInstrument_iface, lpcGUID, ppobj);
133 }
134
135 static HRESULT read_from_stream(IStream *stream, void *data, ULONG size)
136 {
137     ULONG bytes_read;
138     HRESULT hr;
139
140     hr = IStream_Read(stream, data, size, &bytes_read);
141     if(FAILED(hr)){
142         TRACE("IStream_Read failed: %08x\n", hr);
143         return hr;
144     }
145     if (bytes_read < size) {
146         TRACE("Didn't read full chunk: %u < %u\n", bytes_read, size);
147         return E_FAIL;
148     }
149
150     return S_OK;
151 }
152
153 static inline DWORD subtract_bytes(DWORD len, DWORD bytes)
154 {
155     if(bytes > len){
156         TRACE("Apparent mismatch in chunk lengths? %u bytes remaining, %u bytes read\n", len, bytes);
157         return 0;
158     }
159     return len - bytes;
160 }
161
162 static inline HRESULT advance_stream(IStream *stream, ULONG bytes)
163 {
164     LARGE_INTEGER move;
165     HRESULT ret;
166
167     move.QuadPart = bytes;
168
169     ret = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
170     if (FAILED(ret))
171         WARN("IStream_Seek failed: %08x\n", ret);
172
173     return ret;
174 }
175
176 static HRESULT load_region(IDirectMusicInstrumentImpl *This, IStream *stream, instrument_region *region, ULONG length)
177 {
178     HRESULT ret;
179     DMUS_PRIVATE_CHUNK chunk;
180
181     TRACE("(%p, %p, %p, %u)\n", This, stream, region, length);
182
183     while (length)
184     {
185         ret = read_from_stream(stream, &chunk, sizeof(chunk));
186         if (FAILED(ret))
187             return ret;
188
189         length = subtract_bytes(length, sizeof(chunk));
190
191         switch (chunk.fccID)
192         {
193             case FOURCC_RGNH:
194                 TRACE("RGNH chunk (region header): %u bytes\n", chunk.dwSize);
195
196                 ret = read_from_stream(stream, &region->header, sizeof(region->header));
197                 if (FAILED(ret))
198                     return ret;
199
200                 length = subtract_bytes(length, sizeof(region->header));
201                 break;
202
203             case FOURCC_WSMP:
204                 TRACE("WSMP chunk (wave sample): %u bytes\n", chunk.dwSize);
205
206                 ret = read_from_stream(stream, &region->wave_sample, sizeof(region->wave_sample));
207                 if (FAILED(ret))
208                     return ret;
209                 length = subtract_bytes(length, sizeof(region->wave_sample));
210
211                 if (!(region->loop_present = (chunk.dwSize != sizeof(region->wave_sample))))
212                     break;
213
214                 ret = read_from_stream(stream, &region->wave_loop, sizeof(region->wave_loop));
215                 if (FAILED(ret))
216                     return ret;
217
218                 length = subtract_bytes(length, sizeof(region->wave_loop));
219                 break;
220
221             case FOURCC_WLNK:
222                 TRACE("WLNK chunk (wave link): %u bytes\n", chunk.dwSize);
223
224                 ret = read_from_stream(stream, &region->wave_link, sizeof(region->wave_link));
225                 if (FAILED(ret))
226                     return ret;
227
228                 length = subtract_bytes(length, sizeof(region->wave_link));
229                 break;
230
231             default:
232                 TRACE("Unknown chunk %s (skipping): %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
233
234                 ret = advance_stream(stream, chunk.dwSize);
235                 if (FAILED(ret))
236                     return ret;
237
238                 length = subtract_bytes(length, chunk.dwSize);
239                 break;
240         }
241     }
242
243     return S_OK;
244 }
245
246 /* Function that loads all instrument data and which is called from IDirectMusicCollection_GetInstrument as in native */
247 HRESULT IDirectMusicInstrumentImpl_CustomLoad(IDirectMusicInstrument *iface, IStream *stream)
248 {
249     IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
250     HRESULT hr;
251     DMUS_PRIVATE_CHUNK chunk;
252     ULONG i = 0;
253     ULONG length = This->length;
254
255     TRACE("(%p, %p): offset = 0x%s, length = %u)\n", This, stream, wine_dbgstr_longlong(This->liInstrumentPosition.QuadPart), This->length);
256
257     if (This->loaded)
258         return S_OK;
259
260     hr = IStream_Seek(stream, This->liInstrumentPosition, STREAM_SEEK_SET, NULL);
261     if (FAILED(hr))
262     {
263         WARN("IStream_Seek failed: %08x\n", hr);
264         return DMUS_E_UNSUPPORTED_STREAM;
265     }
266
267     This->regions = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->regions) * This->header.cRegions);
268     if (!This->regions)
269         return E_OUTOFMEMORY;
270
271     while (length)
272     {
273         hr = read_from_stream(stream, &chunk, sizeof(chunk));
274         if (FAILED(hr))
275             goto error;
276
277         length = subtract_bytes(length, sizeof(chunk) + chunk.dwSize);
278
279         switch (chunk.fccID)
280         {
281             case FOURCC_INSH:
282             case FOURCC_DLID:
283                 TRACE("Chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
284
285                 /* Instrument header and id are already set so just skip */
286                 hr = advance_stream(stream, chunk.dwSize);
287                 if (FAILED(hr))
288                     goto error;
289
290                 break;
291
292             case FOURCC_LIST: {
293                 DWORD size = chunk.dwSize;
294
295                 TRACE("LIST chunk: %u bytes\n", chunk.dwSize);
296
297                 hr = read_from_stream(stream, &chunk.fccID, sizeof(chunk.fccID));
298                 if (FAILED(hr))
299                     goto error;
300
301                 size = subtract_bytes(size, sizeof(chunk.fccID));
302
303                 switch (chunk.fccID)
304                 {
305                     case FOURCC_LRGN:
306                         TRACE("LRGN chunk (regions list): %u bytes\n", size);
307
308                         while (size)
309                         {
310                             hr = read_from_stream(stream, &chunk, sizeof(chunk));
311                             if (FAILED(hr))
312                                 goto error;
313
314                             if (chunk.fccID != FOURCC_LIST)
315                             {
316                                 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
317                                 goto error;
318                             }
319
320                             hr = read_from_stream(stream, &chunk.fccID, sizeof(chunk.fccID));
321                             if (FAILED(hr))
322                                 goto error;
323
324                             if (chunk.fccID == FOURCC_RGN)
325                             {
326                                 TRACE("RGN chunk (region list): %u bytes\n", chunk.dwSize);
327                                 hr = load_region(This, stream, &This->regions[i++], chunk.dwSize - sizeof(chunk.fccID));
328                             }
329                             else
330                             {
331                                 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
332                                 hr = advance_stream(stream, chunk.dwSize - sizeof(chunk.fccID));
333                             }
334                             if (FAILED(hr))
335                                 goto error;
336
337                             size = subtract_bytes(size, chunk.dwSize + sizeof(chunk));
338                         }
339                         break;
340
341                     default:
342                         TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
343
344                         hr = advance_stream(stream, chunk.dwSize - sizeof(chunk.fccID));
345                         if (FAILED(hr))
346                             goto error;
347
348                         size = subtract_bytes(size, chunk.dwSize - sizeof(chunk.fccID));
349                         break;
350                 }
351                 break;
352             }
353
354             default:
355                 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
356
357                 hr = advance_stream(stream, chunk.dwSize);
358                 if (FAILED(hr))
359                     goto error;
360
361                 break;
362         }
363     }
364
365     This->loaded = TRUE;
366
367     return S_OK;
368
369 error:
370     HeapFree(GetProcessHeap(), 0, This->regions);
371     This->regions = NULL;
372
373     return DMUS_E_UNSUPPORTED_STREAM;
374 }