msctf: Spelling fix.
[wine.git] / dlls / dmstyle / style.c
blob255c96a1743371ffc02e079c51d4b85150148a78
1 /* IDirectMusicStyle8 Implementation
3 * Copyright (C) 2003-2004 Rok Mandeljc
4 * Copyright (C) 2003-2004 Raphael Junqueira
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 "dmstyle_private.h"
22 #include "dmobject.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(dmstyle);
25 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
27 /*****************************************************************************
28 * IDirectMusicStyleImpl implementation
30 typedef struct IDirectMusicStyle8Impl {
31 IDirectMusicStyle8 IDirectMusicStyle8_iface;
32 struct dmobject dmobj;
33 LONG ref;
34 DMUS_IO_STYLE style;
35 struct list Motifs;
36 struct list Bands;
37 } IDirectMusicStyle8Impl;
39 static inline IDirectMusicStyle8Impl *impl_from_IDirectMusicStyle8(IDirectMusicStyle8 *iface)
41 return CONTAINING_RECORD(iface, IDirectMusicStyle8Impl, IDirectMusicStyle8_iface);
44 /* DirectMusicStyle8Impl IDirectMusicStyle8 part: */
45 static HRESULT WINAPI IDirectMusicStyle8Impl_QueryInterface(IDirectMusicStyle8 *iface, REFIID riid,
46 void **ret_iface)
48 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
50 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
52 *ret_iface = NULL;
54 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDirectMusicStyle) ||
55 IsEqualIID(riid, &IID_IDirectMusicStyle8))
56 *ret_iface = iface;
57 else if (IsEqualIID(riid, &IID_IDirectMusicObject))
58 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
59 else if (IsEqualIID(riid, &IID_IPersistStream))
60 *ret_iface = &This->dmobj.IPersistStream_iface;
61 else {
62 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
63 return E_NOINTERFACE;
66 IUnknown_AddRef((IUnknown*)*ret_iface);
67 return S_OK;
70 static ULONG WINAPI IDirectMusicStyle8Impl_AddRef(IDirectMusicStyle8 *iface)
72 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
73 LONG ref = InterlockedIncrement(&This->ref);
75 TRACE("(%p) ref=%d\n", This, ref);
77 return ref;
80 static ULONG WINAPI IDirectMusicStyle8Impl_Release(IDirectMusicStyle8 *iface)
82 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
83 LONG ref = InterlockedDecrement(&This->ref);
85 TRACE("(%p) ref=%d\n", This, ref);
87 if (!ref) {
88 HeapFree(GetProcessHeap(), 0, This);
89 DMSTYLE_UnlockModule();
92 return ref;
95 /* IDirectMusicStyle8Impl IDirectMusicStyle(8) part: */
96 static HRESULT WINAPI IDirectMusicStyle8Impl_GetBand(IDirectMusicStyle8 *iface, WCHAR *pwszName,
97 IDirectMusicBand **ppBand)
99 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
100 FIXME("(%p, %p, %p): stub\n", This, pwszName, ppBand);
101 return S_OK;
104 static HRESULT WINAPI IDirectMusicStyle8Impl_EnumBand(IDirectMusicStyle8 *iface, DWORD dwIndex,
105 WCHAR *pwszName)
107 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
108 FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
109 return S_OK;
112 static HRESULT WINAPI IDirectMusicStyle8Impl_GetDefaultBand(IDirectMusicStyle8 *iface,
113 IDirectMusicBand **ppBand)
115 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
116 FIXME("(%p, %p): stub\n", This, ppBand);
117 return S_OK;
120 static HRESULT WINAPI IDirectMusicStyle8Impl_EnumMotif(IDirectMusicStyle8 *iface, DWORD dwIndex,
121 WCHAR *pwszName)
123 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
124 FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
125 return S_OK;
128 static HRESULT WINAPI IDirectMusicStyle8Impl_GetMotif(IDirectMusicStyle8 *iface, WCHAR *pwszName,
129 IDirectMusicSegment **ppSegment)
131 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
132 FIXME("(%p, %p, %p): stub\n", This, pwszName, ppSegment);
133 return S_OK;
136 static HRESULT WINAPI IDirectMusicStyle8Impl_GetDefaultChordMap(IDirectMusicStyle8 *iface,
137 IDirectMusicChordMap **ppChordMap)
139 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
140 FIXME("(%p, %p): stub\n", This, ppChordMap);
141 return S_OK;
144 static HRESULT WINAPI IDirectMusicStyle8Impl_EnumChordMap(IDirectMusicStyle8 *iface, DWORD dwIndex,
145 WCHAR *pwszName)
147 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
148 FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
149 return S_OK;
152 static HRESULT WINAPI IDirectMusicStyle8Impl_GetChordMap(IDirectMusicStyle8 *iface, WCHAR *pwszName,
153 IDirectMusicChordMap **ppChordMap)
155 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
156 FIXME("(%p, %p, %p): stub\n", This, pwszName, ppChordMap);
157 return S_OK;
160 static HRESULT WINAPI IDirectMusicStyle8Impl_GetTimeSignature(IDirectMusicStyle8 *iface,
161 DMUS_TIMESIGNATURE *pTimeSig)
163 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
164 FIXME("(%p, %p): stub\n", This, pTimeSig);
165 return S_OK;
168 static HRESULT WINAPI IDirectMusicStyle8Impl_GetEmbellishmentLength(IDirectMusicStyle8 *iface,
169 DWORD dwType, DWORD dwLevel, DWORD *pdwMin, DWORD *pdwMax)
171 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
172 FIXME("(%p, %d, %d, %p, %p): stub\n", This, dwType, dwLevel, pdwMin, pdwMax);
173 return S_OK;
176 static HRESULT WINAPI IDirectMusicStyle8Impl_GetTempo(IDirectMusicStyle8 *iface, double *pTempo)
178 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
179 FIXME("(%p, %p): stub\n", This, pTempo);
180 return S_OK;
183 static HRESULT WINAPI IDirectMusicStyle8Impl_EnumPattern(IDirectMusicStyle8 *iface, DWORD dwIndex,
184 DWORD dwPatternType, WCHAR *pwszName)
186 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
187 FIXME("(%p, %d, %d, %p): stub\n", This, dwIndex, dwPatternType, pwszName);
188 return S_OK;
191 static const IDirectMusicStyle8Vtbl dmstyle8_vtbl = {
192 IDirectMusicStyle8Impl_QueryInterface,
193 IDirectMusicStyle8Impl_AddRef,
194 IDirectMusicStyle8Impl_Release,
195 IDirectMusicStyle8Impl_GetBand,
196 IDirectMusicStyle8Impl_EnumBand,
197 IDirectMusicStyle8Impl_GetDefaultBand,
198 IDirectMusicStyle8Impl_EnumMotif,
199 IDirectMusicStyle8Impl_GetMotif,
200 IDirectMusicStyle8Impl_GetDefaultChordMap,
201 IDirectMusicStyle8Impl_EnumChordMap,
202 IDirectMusicStyle8Impl_GetChordMap,
203 IDirectMusicStyle8Impl_GetTimeSignature,
204 IDirectMusicStyle8Impl_GetEmbellishmentLength,
205 IDirectMusicStyle8Impl_GetTempo,
206 IDirectMusicStyle8Impl_EnumPattern
209 /* IDirectMusicStyle8Impl IDirectMusicObject part: */
210 static inline IDirectMusicStyle8Impl *impl_from_IDirectMusicObject(IDirectMusicObject *iface)
212 return CONTAINING_RECORD(iface, IDirectMusicStyle8Impl, dmobj.IDirectMusicObject_iface);
215 static HRESULT WINAPI IDirectMusicObjectImpl_ParseDescriptor(IDirectMusicObject *iface,
216 IStream *pStream, DMUS_OBJECTDESC *pDesc)
218 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicObject(iface);
219 DMUS_PRIVATE_CHUNK Chunk;
220 DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
221 LARGE_INTEGER liMove; /* used when skipping chunks */
223 TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
225 /* FIXME: should this be determined from stream? */
226 pDesc->dwValidData |= DMUS_OBJ_CLASS;
227 pDesc->guidClass = CLSID_DirectMusicStyle;
229 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
230 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
231 switch (Chunk.fccID) {
232 case FOURCC_RIFF: {
233 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
234 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
235 StreamSize = Chunk.dwSize - sizeof(FOURCC);
236 StreamCount = 0;
237 if (Chunk.fccID == DMUS_FOURCC_STYLE_FORM) {
238 TRACE_(dmfile)(": style form\n");
239 do {
240 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
241 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
242 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
243 switch (Chunk.fccID) {
244 case DMUS_FOURCC_GUID_CHUNK: {
245 TRACE_(dmfile)(": GUID chunk\n");
246 pDesc->dwValidData |= DMUS_OBJ_OBJECT;
247 IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
248 break;
250 case DMUS_FOURCC_VERSION_CHUNK: {
251 TRACE_(dmfile)(": version chunk\n");
252 pDesc->dwValidData |= DMUS_OBJ_VERSION;
253 IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
254 break;
256 case DMUS_FOURCC_CATEGORY_CHUNK: {
257 TRACE_(dmfile)(": category chunk\n");
258 pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
259 IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
260 break;
262 case FOURCC_LIST: {
263 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
264 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
265 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
266 ListCount[0] = 0;
267 switch (Chunk.fccID) {
268 /* evil M$ UNFO list, which can (!?) contain INFO elements */
269 case DMUS_FOURCC_UNFO_LIST: {
270 TRACE_(dmfile)(": UNFO list\n");
271 do {
272 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
273 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
274 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
275 switch (Chunk.fccID) {
276 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
277 (though strings seem to be valid unicode) */
278 case mmioFOURCC('I','N','A','M'):
279 case DMUS_FOURCC_UNAM_CHUNK: {
280 TRACE_(dmfile)(": name chunk\n");
281 pDesc->dwValidData |= DMUS_OBJ_NAME;
282 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
283 break;
285 case mmioFOURCC('I','A','R','T'):
286 case DMUS_FOURCC_UART_CHUNK: {
287 TRACE_(dmfile)(": artist chunk (ignored)\n");
288 liMove.QuadPart = Chunk.dwSize;
289 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
290 break;
292 case mmioFOURCC('I','C','O','P'):
293 case DMUS_FOURCC_UCOP_CHUNK: {
294 TRACE_(dmfile)(": copyright chunk (ignored)\n");
295 liMove.QuadPart = Chunk.dwSize;
296 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
297 break;
299 case mmioFOURCC('I','S','B','J'):
300 case DMUS_FOURCC_USBJ_CHUNK: {
301 TRACE_(dmfile)(": subject chunk (ignored)\n");
302 liMove.QuadPart = Chunk.dwSize;
303 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
304 break;
306 case mmioFOURCC('I','C','M','T'):
307 case DMUS_FOURCC_UCMT_CHUNK: {
308 TRACE_(dmfile)(": comment chunk (ignored)\n");
309 liMove.QuadPart = Chunk.dwSize;
310 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
311 break;
313 default: {
314 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
315 liMove.QuadPart = Chunk.dwSize;
316 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
317 break;
320 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
321 } while (ListCount[0] < ListSize[0]);
322 break;
324 default: {
325 TRACE_(dmfile)(": unknown (skipping)\n");
326 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
327 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
328 break;
331 break;
333 default: {
334 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
335 liMove.QuadPart = Chunk.dwSize;
336 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
337 break;
340 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
341 } while (StreamCount < StreamSize);
342 } else {
343 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
344 liMove.QuadPart = StreamSize;
345 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
346 return E_FAIL;
349 TRACE_(dmfile)(": reading finished\n");
350 break;
352 default: {
353 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
354 liMove.QuadPart = Chunk.dwSize;
355 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
356 return DMUS_E_INVALIDFILE;
360 TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
362 return S_OK;
365 static const IDirectMusicObjectVtbl dmobject_vtbl = {
366 dmobj_IDirectMusicObject_QueryInterface,
367 dmobj_IDirectMusicObject_AddRef,
368 dmobj_IDirectMusicObject_Release,
369 dmobj_IDirectMusicObject_GetDescriptor,
370 dmobj_IDirectMusicObject_SetDescriptor,
371 IDirectMusicObjectImpl_ParseDescriptor
374 /* IDirectMusicStyle8Impl IPersistStream part: */
375 static inline IDirectMusicStyle8Impl *impl_from_IPersistStream(IPersistStream *iface)
377 return CONTAINING_RECORD(iface, IDirectMusicStyle8Impl, dmobj.IPersistStream_iface);
380 static HRESULT load_band(IStream *pClonedStream, IDirectMusicBand **ppBand)
382 HRESULT hr = E_FAIL;
383 IPersistStream* pPersistStream = NULL;
385 hr = CoCreateInstance (&CLSID_DirectMusicBand, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicBand, (LPVOID*) ppBand);
386 if (FAILED(hr)) {
387 ERR(": could not create object\n");
388 return hr;
390 /* acquire PersistStream interface */
391 hr = IDirectMusicBand_QueryInterface (*ppBand, &IID_IPersistStream, (LPVOID*) &pPersistStream);
392 if (FAILED(hr)) {
393 ERR(": could not acquire IPersistStream\n");
394 return hr;
396 /* load */
397 hr = IPersistStream_Load (pPersistStream, pClonedStream);
398 if (FAILED(hr)) {
399 ERR(": failed to load object\n");
400 return hr;
403 /* release all loading-related stuff */
404 IPersistStream_Release (pPersistStream);
406 return S_OK;
409 static HRESULT parse_part_ref_list(DMUS_PRIVATE_CHUNK *pChunk, IStream *pStm,
410 DMUS_PRIVATE_STYLE_MOTIF *pNewMotif)
412 HRESULT hr = E_FAIL;
413 DMUS_PRIVATE_CHUNK Chunk;
414 DWORD ListSize[3], ListCount[3];
415 LARGE_INTEGER liMove; /* used when skipping chunks */
417 LPDMUS_PRIVATE_STYLE_PARTREF_ITEM pNewItem = NULL;
420 if (pChunk->fccID != DMUS_FOURCC_PARTREF_LIST) {
421 ERR_(dmfile)(": %s chunk should be a PARTREF list\n", debugstr_fourcc (pChunk->fccID));
422 return E_FAIL;
425 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
426 ListCount[0] = 0;
428 do {
429 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
430 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
431 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
432 switch (Chunk.fccID) {
433 case DMUS_FOURCC_PARTREF_CHUNK: {
434 TRACE_(dmfile)(": PartRef chunk\n");
435 pNewItem = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_PARTREF_ITEM));
436 if (!pNewItem) {
437 ERR(": no more memory\n");
438 return E_OUTOFMEMORY;
440 hr = IStream_Read (pStm, &pNewItem->part_ref, sizeof(DMUS_IO_PARTREF), NULL);
441 /*TRACE_(dmfile)(" - sizeof %lu\n", sizeof(DMUS_IO_PARTREF));*/
442 list_add_tail (&pNewMotif->Items, &pNewItem->entry);
443 pNewItem->desc.dwSize = sizeof(pNewItem->desc);
444 break;
446 case FOURCC_LIST: {
447 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
448 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
449 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
450 ListCount[1] = 0;
451 switch (Chunk.fccID) {
452 case DMUS_FOURCC_UNFO_LIST: {
453 TRACE_(dmfile)(": UNFO list\n");
454 do {
455 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
456 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
457 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
459 if (!pNewItem) {
460 ERR(": pNewItem not yet allocated, chunk order bad?\n");
461 return E_OUTOFMEMORY;
463 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &pNewItem->desc);
464 if (FAILED(hr)) return hr;
466 if (hr == S_FALSE) {
467 switch (Chunk.fccID) {
468 default: {
469 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
470 liMove.QuadPart = Chunk.dwSize;
471 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
472 break;
476 TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
477 } while (ListCount[1] < ListSize[1]);
478 break;
480 default: {
481 TRACE_(dmfile)(": unknown chunk (skipping)\n");
482 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
483 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
484 break;
487 break;
489 default: {
490 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
491 liMove.QuadPart = Chunk.dwSize;
492 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
493 break;
496 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
497 } while (ListCount[0] < ListSize[0]);
499 return S_OK;
502 static HRESULT parse_part_list(DMUS_PRIVATE_CHUNK *pChunk, IStream *pStm)
504 HRESULT hr = E_FAIL;
505 DMUS_PRIVATE_CHUNK Chunk;
506 DWORD ListSize[3], ListCount[3];
507 LARGE_INTEGER liMove; /* used when skipping chunks */
509 DMUS_OBJECTDESC desc;
510 DWORD dwSize = 0;
511 DWORD cnt = 0;
513 if (pChunk->fccID != DMUS_FOURCC_PART_LIST) {
514 ERR_(dmfile)(": %s chunk should be a PART list\n", debugstr_fourcc (pChunk->fccID));
515 return E_FAIL;
518 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
519 ListCount[0] = 0;
521 do {
522 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
523 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
524 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
525 switch (Chunk.fccID) {
526 case DMUS_FOURCC_PART_CHUNK: {
527 TRACE_(dmfile)(": Part chunk (skipping for now)\n" );
528 liMove.QuadPart = Chunk.dwSize;
529 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
530 break;
532 case DMUS_FOURCC_NOTE_CHUNK: {
533 TRACE_(dmfile)(": Note chunk (skipping for now)\n");
534 IStream_Read (pStm, &dwSize, sizeof(DWORD), NULL);
535 cnt = (Chunk.dwSize - sizeof(DWORD));
536 TRACE_(dmfile)(" - dwSize: %u\n", dwSize);
537 TRACE_(dmfile)(" - cnt: %u (%u / %u)\n", cnt / dwSize, (DWORD)(Chunk.dwSize - sizeof(DWORD)), dwSize);
538 if (cnt % dwSize != 0) {
539 ERR("Invalid Array Size\n");
540 return E_FAIL;
542 cnt /= dwSize;
543 /** skip for now */
544 liMove.QuadPart = cnt * dwSize;
545 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
546 break;
548 case DMUS_FOURCC_CURVE_CHUNK: {
549 TRACE_(dmfile)(": Curve chunk (skipping for now)\n");
550 IStream_Read (pStm, &dwSize, sizeof(DWORD), NULL);
551 cnt = (Chunk.dwSize - sizeof(DWORD));
552 TRACE_(dmfile)(" - dwSize: %u\n", dwSize);
553 TRACE_(dmfile)(" - cnt: %u (%u / %u)\n", cnt / dwSize, (DWORD)(Chunk.dwSize - sizeof(DWORD)), dwSize);
554 if (cnt % dwSize != 0) {
555 ERR("Invalid Array Size\n");
556 return E_FAIL;
558 cnt /= dwSize;
559 /** skip for now */
560 liMove.QuadPart = cnt * dwSize;
561 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
562 break;
564 case DMUS_FOURCC_MARKER_CHUNK: {
565 TRACE_(dmfile)(": Marker chunk (skipping for now)\n");
566 liMove.QuadPart = Chunk.dwSize;
567 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
568 break;
570 case DMUS_FOURCC_RESOLUTION_CHUNK: {
571 TRACE_(dmfile)(": Resolution chunk (skipping for now)\n");
572 liMove.QuadPart = Chunk.dwSize;
573 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
574 break;
576 case DMUS_FOURCC_ANTICIPATION_CHUNK: {
577 TRACE_(dmfile)(": Anticipation chunk (skipping for now)\n");
578 liMove.QuadPart = Chunk.dwSize;
579 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
580 break;
582 case FOURCC_LIST: {
583 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
584 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
585 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
586 ListCount[1] = 0;
587 switch (Chunk.fccID) {
588 case DMUS_FOURCC_UNFO_LIST: {
589 TRACE_(dmfile)(": UNFO list\n");
590 do {
591 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
592 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
593 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
595 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &desc);
596 if (FAILED(hr)) return hr;
598 if (hr == S_FALSE) {
599 switch (Chunk.fccID) {
600 default: {
601 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
602 liMove.QuadPart = Chunk.dwSize;
603 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
604 break;
608 TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
609 } while (ListCount[1] < ListSize[1]);
610 break;
612 default: {
613 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
614 liMove.QuadPart = Chunk.dwSize;
615 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
616 break;
619 break;
621 default: {
622 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
623 liMove.QuadPart = Chunk.dwSize;
624 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
625 break;
628 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
629 } while (ListCount[0] < ListSize[0]);
631 return S_OK;
634 static HRESULT parse_pattern_list(IDirectMusicStyle8Impl *This, DMUS_PRIVATE_CHUNK *pChunk,
635 IStream *pStm)
637 HRESULT hr = E_FAIL;
638 DMUS_PRIVATE_CHUNK Chunk;
639 DWORD ListSize[3], ListCount[3];
640 LARGE_INTEGER liMove; /* used when skipping chunks */
642 IDirectMusicBand* pBand = NULL;
643 LPDMUS_PRIVATE_STYLE_MOTIF pNewMotif = NULL;
645 if (pChunk->fccID != DMUS_FOURCC_PATTERN_LIST) {
646 ERR_(dmfile)(": %s chunk should be a PATTERN list\n", debugstr_fourcc (pChunk->fccID));
647 return E_FAIL;
650 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
651 ListCount[0] = 0;
653 do {
654 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
655 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
656 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
657 switch (Chunk.fccID) {
658 case DMUS_FOURCC_PATTERN_CHUNK: {
659 TRACE_(dmfile)(": Pattern chunk\n");
660 /** alloc new motif entry */
661 pNewMotif = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_MOTIF));
662 if (NULL == pNewMotif) {
663 ERR(": no more memory\n");
664 return E_OUTOFMEMORY;
666 list_add_tail (&This->Motifs, &pNewMotif->entry);
668 IStream_Read (pStm, &pNewMotif->pattern, Chunk.dwSize, NULL);
669 /** TODO trace pattern */
671 /** reset all data, as a new pattern begin */
672 pNewMotif->desc.dwSize = sizeof(pNewMotif->desc);
673 list_init (&pNewMotif->Items);
674 break;
676 case DMUS_FOURCC_RHYTHM_CHUNK: {
677 TRACE_(dmfile)(": Rhythm chunk\n");
678 IStream_Read (pStm, &pNewMotif->dwRhythm, sizeof(DWORD), NULL);
679 TRACE_(dmfile)(" - dwRhythm: %u\n", pNewMotif->dwRhythm);
680 /** TODO understand why some Chunks have size > 4 */
681 liMove.QuadPart = Chunk.dwSize - sizeof(DWORD);
682 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
683 break;
685 case DMUS_FOURCC_MOTIFSETTINGS_CHUNK: {
686 TRACE_(dmfile)(": MotifSettings chunk (skipping for now)\n");
687 IStream_Read (pStm, &pNewMotif->settings, Chunk.dwSize, NULL);
688 /** TODO trace settings */
689 break;
691 case FOURCC_RIFF: {
693 * should be embedded Bands into pattern
695 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
696 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
697 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
698 ListCount[1] = 0;
699 switch (Chunk.fccID) {
700 case DMUS_FOURCC_BAND_FORM: {
701 LPSTREAM pClonedStream = NULL;
703 TRACE_(dmfile)(": BAND RIFF\n");
705 IStream_Clone (pStm, &pClonedStream);
707 liMove.QuadPart = 0;
708 liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
709 IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
711 hr = load_band(pClonedStream, &pBand);
712 if (FAILED(hr)) {
713 ERR(": could not load track\n");
714 return hr;
716 IStream_Release (pClonedStream);
718 pNewMotif->pBand = pBand;
719 IDirectMusicBand_AddRef(pBand);
721 IDirectMusicTrack_Release(pBand); pBand = NULL; /* now we can release it as it's inserted */
723 /** now safe move the cursor */
724 liMove.QuadPart = ListSize[1];
725 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
727 break;
729 default: {
730 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
731 liMove.QuadPart = ListSize[1];
732 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
733 break;
736 break;
738 case FOURCC_LIST: {
739 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
740 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
741 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
742 ListCount[1] = 0;
743 switch (Chunk.fccID) {
744 case DMUS_FOURCC_UNFO_LIST: {
745 TRACE_(dmfile)(": UNFO list\n");
746 do {
747 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
748 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
749 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
751 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &pNewMotif->desc);
752 if (FAILED(hr)) return hr;
754 if (hr == S_FALSE) {
755 switch (Chunk.fccID) {
756 default: {
757 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
758 liMove.QuadPart = Chunk.dwSize;
759 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
760 break;
764 TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
765 } while (ListCount[1] < ListSize[1]);
766 break;
768 case DMUS_FOURCC_PARTREF_LIST: {
769 TRACE_(dmfile)(": PartRef list\n");
770 hr = parse_part_ref_list(&Chunk, pStm, pNewMotif);
771 if (FAILED(hr)) return hr;
772 break;
774 default: {
775 TRACE_(dmfile)(": unknown (skipping)\n");
776 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
777 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
778 break;
781 break;
783 default: {
784 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
785 liMove.QuadPart = Chunk.dwSize;
786 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
787 break;
790 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
791 } while (ListCount[0] < ListSize[0]);
793 return S_OK;
796 static HRESULT parse_style_form(IDirectMusicStyle8Impl *This, DMUS_PRIVATE_CHUNK *pChunk,
797 IStream *pStm)
799 HRESULT hr = E_FAIL;
800 DMUS_PRIVATE_CHUNK Chunk;
801 DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
802 LARGE_INTEGER liMove; /* used when skipping chunks */
804 IDirectMusicBand* pBand = NULL;
806 if (pChunk->fccID != DMUS_FOURCC_STYLE_FORM) {
807 ERR_(dmfile)(": %s chunk should be a STYLE form\n", debugstr_fourcc (pChunk->fccID));
808 return E_FAIL;
811 StreamSize = pChunk->dwSize - sizeof(FOURCC);
812 StreamCount = 0;
814 do {
815 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
816 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
817 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
819 hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, &This->dmobj.desc);
820 if (FAILED(hr)) return hr;
822 if (hr == S_FALSE) {
823 switch (Chunk.fccID) {
824 case DMUS_FOURCC_STYLE_CHUNK: {
825 TRACE_(dmfile)(": Style chunk\n");
826 IStream_Read (pStm, &This->style, sizeof(DMUS_IO_STYLE), NULL);
827 /** TODO dump DMUS_IO_TIMESIG style.timeSig */
828 TRACE_(dmfile)(" - dblTempo: %g\n", This->style.dblTempo);
829 break;
831 case FOURCC_RIFF: {
833 * should be embedded Bands into style
835 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
836 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
837 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
838 ListCount[0] = 0;
839 switch (Chunk.fccID) {
840 case DMUS_FOURCC_BAND_FORM: {
841 LPSTREAM pClonedStream = NULL;
842 LPDMUS_PRIVATE_STYLE_BAND pNewBand;
844 TRACE_(dmfile)(": BAND RIFF\n");
846 IStream_Clone (pStm, &pClonedStream);
848 liMove.QuadPart = 0;
849 liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
850 IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
852 hr = load_band(pClonedStream, &pBand);
853 if (FAILED(hr)) {
854 ERR(": could not load track\n");
855 return hr;
857 IStream_Release (pClonedStream);
859 pNewBand = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_BAND));
860 if (NULL == pNewBand) {
861 ERR(": no more memory\n");
862 return E_OUTOFMEMORY;
864 pNewBand->pBand = pBand;
865 IDirectMusicBand_AddRef(pBand);
866 list_add_tail (&This->Bands, &pNewBand->entry);
868 IDirectMusicTrack_Release(pBand); pBand = NULL; /* now we can release it as it's inserted */
870 /** now safely move the cursor */
871 liMove.QuadPart = ListSize[0];
872 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
874 break;
876 default: {
877 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
878 liMove.QuadPart = ListSize[0];
879 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
880 break;
883 break;
885 case FOURCC_LIST: {
886 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
887 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
888 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
889 ListCount[0] = 0;
890 switch (Chunk.fccID) {
891 case DMUS_FOURCC_UNFO_LIST: {
892 TRACE_(dmfile)(": UNFO list\n");
893 do {
894 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
895 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
896 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
898 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &This->dmobj.desc);
899 if (FAILED(hr)) return hr;
901 if (hr == S_FALSE) {
902 switch (Chunk.fccID) {
903 default: {
904 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
905 liMove.QuadPart = Chunk.dwSize;
906 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
907 break;
911 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
912 } while (ListCount[0] < ListSize[0]);
913 break;
915 case DMUS_FOURCC_PART_LIST: {
916 TRACE_(dmfile)(": PART list\n");
917 hr = parse_part_list(&Chunk, pStm);
918 if (FAILED(hr)) return hr;
919 break;
921 case DMUS_FOURCC_PATTERN_LIST: {
922 TRACE_(dmfile)(": PATTERN list\n");
923 hr = parse_pattern_list(This, &Chunk, pStm);
924 if (FAILED(hr)) return hr;
925 break;
927 default: {
928 TRACE_(dmfile)(": unknown (skipping)\n");
929 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
930 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
931 break;
934 break;
936 default: {
937 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
938 liMove.QuadPart = Chunk.dwSize;
939 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
940 break;
944 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
945 } while (StreamCount < StreamSize);
947 return S_OK;
950 static HRESULT WINAPI IPersistStreamImpl_Load(IPersistStream *iface, IStream *pStm)
952 IDirectMusicStyle8Impl *This = impl_from_IPersistStream(iface);
953 DMUS_PRIVATE_CHUNK Chunk;
954 LARGE_INTEGER liMove; /* used when skipping chunks */
955 HRESULT hr;
957 FIXME("(%p, %p): Loading\n", This, pStm);
959 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
960 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
961 switch (Chunk.fccID) {
962 case FOURCC_RIFF: {
963 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
964 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
965 switch (Chunk.fccID) {
966 case DMUS_FOURCC_STYLE_FORM: {
967 TRACE_(dmfile)(": Style form\n");
968 hr = parse_style_form(This, &Chunk, pStm);
969 if (FAILED(hr)) return hr;
970 break;
972 default: {
973 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
974 liMove.QuadPart = Chunk.dwSize;
975 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
976 return E_FAIL;
979 TRACE_(dmfile)(": reading finished\n");
980 break;
982 default: {
983 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
984 liMove.QuadPart = Chunk.dwSize;
985 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
986 return E_FAIL;
990 return S_OK;
993 static const IPersistStreamVtbl persiststream_vtbl = {
994 dmobj_IPersistStream_QueryInterface,
995 dmobj_IPersistStream_AddRef,
996 dmobj_IPersistStream_Release,
997 dmobj_IPersistStream_GetClassID,
998 unimpl_IPersistStream_IsDirty,
999 IPersistStreamImpl_Load,
1000 unimpl_IPersistStream_Save,
1001 unimpl_IPersistStream_GetSizeMax
1004 /* for ClassFactory */
1005 HRESULT WINAPI create_dmstyle(REFIID lpcGUID, void **ppobj)
1007 IDirectMusicStyle8Impl* obj;
1008 HRESULT hr;
1010 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicStyle8Impl));
1011 if (NULL == obj) {
1012 *ppobj = NULL;
1013 return E_OUTOFMEMORY;
1015 obj->IDirectMusicStyle8_iface.lpVtbl = &dmstyle8_vtbl;
1016 obj->ref = 1;
1017 dmobject_init(&obj->dmobj, &CLSID_DirectMusicStyle, (IUnknown *)&obj->IDirectMusicStyle8_iface);
1018 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
1019 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
1020 list_init (&obj->Bands);
1021 list_init (&obj->Motifs);
1023 DMSTYLE_LockModule();
1024 hr = IDirectMusicStyle8_QueryInterface(&obj->IDirectMusicStyle8_iface, lpcGUID, ppobj);
1025 IDirectMusicStyle8_Release(&obj->IDirectMusicStyle8_iface);
1027 return hr;