notepad: Fix the position of the Encoding combobox.
[wine/multimedia.git] / dlls / dmusic / collection.c
blobfaafd643cf65470b087102e1db799aa84b22401f
1 /* IDirectMusicCollection Implementation
3 * Copyright (C) 2003-2004 Rok Mandeljc
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.
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.
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
20 #include "dmusic_private.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
25 static ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_AddRef (LPUNKNOWN iface);
26 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef (LPDIRECTMUSICCOLLECTION iface);
27 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface);
28 static ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface);
30 /*****************************************************************************
31 * IDirectMusicCollectionImpl implementation
33 /* IDirectMusicCollectionImpl IUnknown part: */
34 static HRESULT WINAPI IDirectMusicCollectionImpl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
35 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
36 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
38 if (IsEqualIID (riid, &IID_IUnknown)) {
39 *ppobj = &This->UnknownVtbl;
40 IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
41 return S_OK;
42 } else if (IsEqualIID (riid, &IID_IDirectMusicCollection)) {
43 *ppobj = &This->CollectionVtbl;
44 IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef ((LPDIRECTMUSICCOLLECTION)&This->CollectionVtbl);
45 return S_OK;
46 } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
47 *ppobj = &This->ObjectVtbl;
48 IDirectMusicCollectionImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);
49 return S_OK;
50 } else if (IsEqualIID (riid, &IID_IPersistStream)) {
51 *ppobj = &This->PersistStreamVtbl;
52 IDirectMusicCollectionImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);
53 return S_OK;
56 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
57 return E_NOINTERFACE;
60 static ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_AddRef (LPUNKNOWN iface) {
61 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
62 ULONG refCount = InterlockedIncrement(&This->ref);
64 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
66 DMUSIC_LockModule();
68 return refCount;
71 static ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_Release (LPUNKNOWN iface) {
72 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, 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 static const IUnknownVtbl DirectMusicCollection_Unknown_Vtbl = {
87 IDirectMusicCollectionImpl_IUnknown_QueryInterface,
88 IDirectMusicCollectionImpl_IUnknown_AddRef,
89 IDirectMusicCollectionImpl_IUnknown_Release
92 /* IDirectMusicCollectionImpl IDirectMusicCollection part: */
93 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_QueryInterface (LPDIRECTMUSICCOLLECTION iface, REFIID riid, LPVOID *ppobj) {
94 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
95 return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
98 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef (LPDIRECTMUSICCOLLECTION iface) {
99 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
100 return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
103 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_Release (LPDIRECTMUSICCOLLECTION iface) {
104 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
105 return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
108 /* IDirectMusicCollection Interface follow: */
109 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_GetInstrument (LPDIRECTMUSICCOLLECTION iface, DWORD dwPatch, IDirectMusicInstrument** ppInstrument) {
110 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
111 DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
112 struct list *listEntry;
113 DWORD dwInstPatch;
115 TRACE("(%p, %d, %p)\n", This, dwPatch, ppInstrument);
117 LIST_FOR_EACH (listEntry, &This->Instruments) {
118 tmpEntry = LIST_ENTRY(listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry);
119 IDirectMusicInstrument_GetPatch (tmpEntry->pInstrument, &dwInstPatch);
120 if (dwPatch == dwInstPatch) {
121 *ppInstrument = tmpEntry->pInstrument;
122 IDirectMusicInstrument_AddRef (tmpEntry->pInstrument);
123 IDirectMusicInstrumentImpl_Custom_Load (tmpEntry->pInstrument, This->pStm); /* load instrument before returning it */
124 TRACE(": returning instrument %p\n", *ppInstrument);
125 return S_OK;
129 TRACE(": instrument not found\n");
131 return DMUS_E_INVALIDPATCH;
134 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_EnumInstrument (LPDIRECTMUSICCOLLECTION iface, DWORD dwIndex, DWORD* pdwPatch, LPWSTR pwszName, DWORD dwNameLen) {
135 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
136 unsigned int r = 0;
137 DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
138 struct list *listEntry;
139 DWORD dwLen;
141 TRACE("(%p, %d, %p, %p, %d)\n", This, dwIndex, pdwPatch, pwszName, dwNameLen);
142 LIST_FOR_EACH (listEntry, &This->Instruments) {
143 tmpEntry = LIST_ENTRY(listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry);
144 if (r == dwIndex) {
145 ICOM_NAME_MULTI (IDirectMusicInstrumentImpl, InstrumentVtbl, tmpEntry->pInstrument, pInstrument);
146 IDirectMusicInstrument_GetPatch (tmpEntry->pInstrument, pdwPatch);
147 if (pwszName) {
148 dwLen = min(strlenW(pInstrument->wszName),dwNameLen-1);
149 memcpy (pwszName, pInstrument->wszName, dwLen * sizeof(WCHAR));
150 pwszName[dwLen] = '\0';
152 return S_OK;
154 r++;
157 return S_FALSE;
160 static const IDirectMusicCollectionVtbl DirectMusicCollection_Collection_Vtbl = {
161 IDirectMusicCollectionImpl_IDirectMusicCollection_QueryInterface,
162 IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef,
163 IDirectMusicCollectionImpl_IDirectMusicCollection_Release,
164 IDirectMusicCollectionImpl_IDirectMusicCollection_GetInstrument,
165 IDirectMusicCollectionImpl_IDirectMusicCollection_EnumInstrument
168 /* IDirectMusicCollectionImpl IDirectMusicObject part: */
169 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
170 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
171 return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
174 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
175 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
176 return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
179 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
180 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
181 return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
184 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
185 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
186 TRACE("(%p, %p)\n", This, pDesc);
187 /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
188 memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
189 return S_OK;
192 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
193 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
194 TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
196 /* According to MSDN, we should copy only given values, not whole struct */
197 if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
198 This->pDesc->guidObject = pDesc->guidObject;
199 if (pDesc->dwValidData & DMUS_OBJ_CLASS)
200 This->pDesc->guidClass = pDesc->guidClass;
201 if (pDesc->dwValidData & DMUS_OBJ_NAME)
202 lstrcpynW(This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
203 if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
204 lstrcpynW(This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
205 if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
206 lstrcpynW(This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
207 if (pDesc->dwValidData & DMUS_OBJ_VERSION)
208 This->pDesc->vVersion = pDesc->vVersion;
209 if (pDesc->dwValidData & DMUS_OBJ_DATE)
210 This->pDesc->ftDate = pDesc->ftDate;
211 if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
212 This->pDesc->llMemLength = pDesc->llMemLength;
213 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, pDesc->llMemLength);
215 if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
216 /* according to MSDN, we copy the stream */
217 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);
220 /* add new flags */
221 This->pDesc->dwValidData |= pDesc->dwValidData;
223 return S_OK;
226 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
227 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
228 DMUS_PRIVATE_CHUNK Chunk;
229 DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
230 LARGE_INTEGER liMove; /* used when skipping chunks */
232 TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
234 /* FIXME: should this be determined from stream? */
235 pDesc->dwValidData |= DMUS_OBJ_CLASS;
236 pDesc->guidClass = CLSID_DirectMusicCollection;
238 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
239 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
240 switch (Chunk.fccID) {
241 case FOURCC_RIFF: {
242 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
243 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
244 StreamSize = Chunk.dwSize - sizeof(FOURCC);
245 StreamCount = 0;
246 if (Chunk.fccID == mmioFOURCC('D','L','S',' ')) {
247 TRACE_(dmfile)(": collection form\n");
248 do {
249 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
250 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
251 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
252 switch (Chunk.fccID) {
253 case FOURCC_DLID: {
254 TRACE_(dmfile)(": GUID chunk\n");
255 pDesc->dwValidData |= DMUS_OBJ_OBJECT;
256 IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
257 break;
259 case DMUS_FOURCC_VERSION_CHUNK: {
260 TRACE_(dmfile)(": version chunk\n");
261 pDesc->dwValidData |= DMUS_OBJ_VERSION;
262 IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
263 break;
265 case DMUS_FOURCC_CATEGORY_CHUNK: {
266 TRACE_(dmfile)(": category chunk\n");
267 pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
268 IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
269 break;
271 case FOURCC_LIST: {
272 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
273 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
274 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
275 ListCount[0] = 0;
276 switch (Chunk.fccID) {
277 /* pure INFO list, such can be found in dls collections */
278 case mmioFOURCC('I','N','F','O'): {
279 TRACE_(dmfile)(": INFO list\n");
280 do {
281 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
282 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
283 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
284 switch (Chunk.fccID) {
285 case mmioFOURCC('I','N','A','M'):{
286 CHAR szName[DMUS_MAX_NAME];
287 TRACE_(dmfile)(": name chunk\n");
288 pDesc->dwValidData |= DMUS_OBJ_NAME;
289 IStream_Read (pStream, szName, Chunk.dwSize, NULL);
290 MultiByteToWideChar (CP_ACP, 0, szName, -1, pDesc->wszName, DMUS_MAX_NAME);
291 if (even_or_odd(Chunk.dwSize)) {
292 ListCount[0] ++;
293 liMove.QuadPart = 1;
294 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
296 break;
298 case mmioFOURCC('I','A','R','T'): {
299 TRACE_(dmfile)(": artist chunk (ignored)\n");
300 if (even_or_odd(Chunk.dwSize)) {
301 ListCount[0] ++;
302 Chunk.dwSize++;
304 liMove.QuadPart = Chunk.dwSize;
305 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
306 break;
308 case mmioFOURCC('I','C','O','P'): {
309 TRACE_(dmfile)(": copyright chunk (ignored)\n");
310 if (even_or_odd(Chunk.dwSize)) {
311 ListCount[0] ++;
312 Chunk.dwSize++;
314 liMove.QuadPart = Chunk.dwSize;
315 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
316 break;
318 case mmioFOURCC('I','S','B','J'): {
319 TRACE_(dmfile)(": subject chunk (ignored)\n");
320 if (even_or_odd(Chunk.dwSize)) {
321 ListCount[0] ++;
322 Chunk.dwSize++;
324 liMove.QuadPart = Chunk.dwSize;
325 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
326 break;
328 case mmioFOURCC('I','C','M','T'): {
329 TRACE_(dmfile)(": comment chunk (ignored)\n");
330 if (even_or_odd(Chunk.dwSize)) {
331 ListCount[0] ++;
332 Chunk.dwSize++;
334 liMove.QuadPart = Chunk.dwSize;
335 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
336 break;
338 default: {
339 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
340 if (even_or_odd(Chunk.dwSize)) {
341 ListCount[0] ++;
342 Chunk.dwSize++;
344 liMove.QuadPart = Chunk.dwSize;
345 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
346 break;
349 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
350 } while (ListCount[0] < ListSize[0]);
351 break;
353 default: {
354 TRACE_(dmfile)(": unknown (skipping)\n");
355 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
356 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
357 break;
360 break;
362 default: {
363 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
364 liMove.QuadPart = Chunk.dwSize;
365 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
366 break;
369 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
370 } while (StreamCount < StreamSize);
371 } else {
372 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
373 liMove.QuadPart = StreamSize;
374 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
375 return E_FAIL;
378 TRACE_(dmfile)(": reading finished\n");
379 break;
381 default: {
382 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
383 liMove.QuadPart = Chunk.dwSize;
384 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
385 return DMUS_E_INVALIDFILE;
389 TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
391 return S_OK;
394 static const IDirectMusicObjectVtbl DirectMusicCollection_Object_Vtbl = {
395 IDirectMusicCollectionImpl_IDirectMusicObject_QueryInterface,
396 IDirectMusicCollectionImpl_IDirectMusicObject_AddRef,
397 IDirectMusicCollectionImpl_IDirectMusicObject_Release,
398 IDirectMusicCollectionImpl_IDirectMusicObject_GetDescriptor,
399 IDirectMusicCollectionImpl_IDirectMusicObject_SetDescriptor,
400 IDirectMusicCollectionImpl_IDirectMusicObject_ParseDescriptor
403 /* IDirectMusicCollectionImpl IPersistStream part: */
404 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
405 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
406 return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
409 static ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
410 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
411 return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
414 static ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
415 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
416 return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
419 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
420 return E_NOTIMPL;
423 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
424 return E_NOTIMPL;
427 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
428 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
430 DMUS_PRIVATE_CHUNK Chunk;
431 DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
432 LARGE_INTEGER liMove; /* used when skipping chunks */
433 ULARGE_INTEGER dlibCollectionPosition, dlibInstrumentPosition, dlibWavePoolPosition;
435 IStream_AddRef (pStm); /* add count for later references */
436 liMove.QuadPart = 0;
437 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibCollectionPosition); /* store offset, in case it'll be needed later */
438 This->liCollectionPosition.QuadPart = dlibCollectionPosition.QuadPart;
439 This->pStm = pStm;
441 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
442 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
443 switch (Chunk.fccID) {
444 case FOURCC_RIFF: {
445 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
446 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
447 StreamSize = Chunk.dwSize - sizeof(FOURCC);
448 StreamCount = 0;
449 switch (Chunk.fccID) {
450 case FOURCC_DLS: {
451 TRACE_(dmfile)(": collection form\n");
452 do {
453 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
454 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
455 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
456 switch (Chunk.fccID) {
457 case FOURCC_COLH: {
458 TRACE_(dmfile)(": collection header chunk\n");
459 This->pHeader = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
460 IStream_Read (pStm, This->pHeader, Chunk.dwSize, NULL);
461 break;
463 case FOURCC_DLID: {
464 TRACE_(dmfile)(": DLID (GUID) chunk\n");
465 This->pDesc->dwValidData |= DMUS_OBJ_OBJECT;
466 IStream_Read (pStm, &This->pDesc->guidObject, Chunk.dwSize, NULL);
467 break;
469 case FOURCC_VERS: {
470 TRACE_(dmfile)(": version chunk\n");
471 This->pDesc->dwValidData |= DMUS_OBJ_VERSION;
472 IStream_Read (pStm, &This->pDesc->vVersion, Chunk.dwSize, NULL);
473 break;
475 case FOURCC_PTBL: {
476 TRACE_(dmfile)(": pool table chunk\n");
477 This->pPoolTable = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(POOLTABLE));
478 IStream_Read (pStm, This->pPoolTable, sizeof(POOLTABLE), NULL);
479 Chunk.dwSize -= sizeof(POOLTABLE);
480 This->pPoolCues = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, This->pPoolTable->cCues*sizeof(POOLCUE));
481 IStream_Read (pStm, This->pPoolCues, Chunk.dwSize, NULL);
482 break;
484 case FOURCC_LIST: {
485 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
486 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
487 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
488 ListCount[0] = 0;
489 switch (Chunk.fccID) {
490 case mmioFOURCC('I','N','F','O'): {
491 TRACE_(dmfile)(": INFO list\n");
492 do {
493 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
494 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
495 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
496 switch (Chunk.fccID) {
497 case mmioFOURCC('I','N','A','M'): {
498 CHAR szName[DMUS_MAX_NAME];
499 TRACE_(dmfile)(": name chunk\n");
500 This->pDesc->dwValidData |= DMUS_OBJ_NAME;
501 IStream_Read (pStm, szName, Chunk.dwSize, NULL);
502 MultiByteToWideChar (CP_ACP, 0, szName, -1, This->pDesc->wszName, DMUS_MAX_NAME);
503 if (even_or_odd(Chunk.dwSize)) {
504 ListCount[0] ++;
505 liMove.QuadPart = 1;
506 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
508 break;
510 case mmioFOURCC('I','A','R','T'): {
511 TRACE_(dmfile)(": artist chunk (ignored)\n");
512 if (even_or_odd(Chunk.dwSize)) {
513 ListCount[0] ++;
514 Chunk.dwSize++;
516 liMove.QuadPart = Chunk.dwSize;
517 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
518 break;
520 case mmioFOURCC('I','C','O','P'): {
521 TRACE_(dmfile)(": copyright chunk\n");
522 This->szCopyright = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
523 IStream_Read (pStm, This->szCopyright, Chunk.dwSize, NULL);
524 if (even_or_odd(Chunk.dwSize)) {
525 ListCount[0] ++;
526 liMove.QuadPart = 1;
527 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
529 break;
531 case mmioFOURCC('I','S','B','J'): {
532 TRACE_(dmfile)(": subject chunk (ignored)\n");
533 if (even_or_odd(Chunk.dwSize)) {
534 ListCount[0] ++;
535 Chunk.dwSize++;
537 liMove.QuadPart = Chunk.dwSize;
538 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
539 break;
541 case mmioFOURCC('I','C','M','T'): {
542 TRACE_(dmfile)(": comment chunk (ignored)\n");
543 if (even_or_odd(Chunk.dwSize)) {
544 ListCount[0] ++;
545 Chunk.dwSize++;
547 liMove.QuadPart = Chunk.dwSize;
548 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
549 break;
551 default: {
552 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
553 if (even_or_odd(Chunk.dwSize)) {
554 ListCount[0] ++;
555 Chunk.dwSize++;
557 liMove.QuadPart = Chunk.dwSize;
558 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
559 break;
562 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
563 } while (ListCount[0] < ListSize[0]);
564 break;
566 case FOURCC_WVPL: {
567 TRACE_(dmfile)(": wave pool list (mark & skip)\n");
568 liMove.QuadPart = 0;
569 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibWavePoolPosition); /* store position */
570 This->liWavePoolTablePosition.QuadPart = dlibWavePoolPosition.QuadPart;
571 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
572 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
573 break;
575 case FOURCC_LINS: {
576 TRACE_(dmfile)(": instruments list\n");
577 do {
578 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
579 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
580 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
581 switch (Chunk.fccID) {
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 FOURCC_INS: {
589 LPDMUS_PRIVATE_INSTRUMENTENTRY pNewInstrument = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_INSTRUMENTENTRY));
590 TRACE_(dmfile)(": instrument list\n");
591 DMUSIC_CreateDirectMusicInstrumentImpl (&IID_IDirectMusicInstrument, (LPVOID*)&pNewInstrument->pInstrument, NULL); /* only way to create this one... even M$ does it discretely */
593 ICOM_NAME_MULTI (IDirectMusicInstrumentImpl, InstrumentVtbl, pNewInstrument->pInstrument, pInstrument);
594 liMove.QuadPart = 0;
595 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibInstrumentPosition);
596 pInstrument->liInstrumentPosition.QuadPart = dlibInstrumentPosition.QuadPart - (2*sizeof(FOURCC) + sizeof(DWORD)); /* store offset, it'll be needed later */
598 do {
599 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
600 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
601 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
602 switch (Chunk.fccID) {
603 case FOURCC_INSH: {
604 TRACE_(dmfile)(": instrument header chunk\n");
605 pInstrument->pHeader = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
606 IStream_Read (pStm, pInstrument->pHeader, Chunk.dwSize, NULL);
607 break;
609 case FOURCC_DLID: {
610 TRACE_(dmfile)(": DLID (GUID) chunk\n");
611 pInstrument->pInstrumentID = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
612 IStream_Read (pStm, pInstrument->pInstrumentID, Chunk.dwSize, NULL);
613 break;
615 case FOURCC_LIST: {
616 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
617 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
618 ListSize[2] = Chunk.dwSize - sizeof(FOURCC);
619 ListCount[2] = 0;
620 switch (Chunk.fccID) {
621 default: {
622 TRACE_(dmfile)(": unknown (skipping)\n");
623 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
624 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
625 break;
628 break;
630 default: {
631 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
632 liMove.QuadPart = Chunk.dwSize;
633 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
634 break;
637 TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
638 } while (ListCount[1] < ListSize[1]);
639 /* DEBUG: dumps whole instrument object tree: */
640 if (TRACE_ON(dmusic)) {
641 TRACE("*** IDirectMusicInstrument (%p) ***\n", pInstrument);
642 if (pInstrument->pInstrumentID)
643 TRACE(" - GUID = %s\n", debugstr_dmguid(pInstrument->pInstrumentID));
645 TRACE(" - Instrument header:\n");
646 TRACE(" - cRegions: %d\n", pInstrument->pHeader->cRegions);
647 TRACE(" - Locale:\n");
648 TRACE(" - ulBank: %d\n", pInstrument->pHeader->Locale.ulBank);
649 TRACE(" - ulInstrument: %d\n", pInstrument->pHeader->Locale.ulInstrument);
650 TRACE(" => dwPatch: %d\n", MIDILOCALE2Patch(&pInstrument->pHeader->Locale));
652 list_add_tail (&This->Instruments, &pNewInstrument->entry);
654 break;
657 break;
659 default: {
660 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
661 liMove.QuadPart = Chunk.dwSize;
662 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
663 break;
666 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
667 } while (ListCount[0] < ListSize[0]);
668 break;
670 default: {
671 TRACE_(dmfile)(": unknown (skipping)\n");
672 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
673 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
674 break;
677 break;
679 default: {
680 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
681 liMove.QuadPart = Chunk.dwSize;
682 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
683 break;
686 TRACE_(dmfile)(": StreamCount = %d < StreamSize = %d\n", StreamCount, StreamSize);
687 } while (StreamCount < StreamSize);
688 break;
690 default: {
691 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
692 liMove.QuadPart = StreamSize;
693 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
694 return E_FAIL;
697 TRACE_(dmfile)(": reading finished\n");
698 break;
700 default: {
701 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
702 liMove.QuadPart = Chunk.dwSize;
703 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
704 return E_FAIL;
708 /* DEBUG: dumps whole collection object tree: */
709 if (TRACE_ON(dmusic)) {
710 int r = 0;
711 DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
712 struct list *listEntry;
714 TRACE("*** IDirectMusicCollection (%p) ***\n", This->CollectionVtbl);
715 if (This->pDesc->dwValidData & DMUS_OBJ_OBJECT)
716 TRACE(" - GUID = %s\n", debugstr_dmguid(&This->pDesc->guidObject));
717 if (This->pDesc->dwValidData & DMUS_OBJ_VERSION)
718 TRACE(" - Version = %i,%i,%i,%i\n", (This->pDesc->vVersion.dwVersionMS >> 8) & 0x0000FFFF, This->pDesc->vVersion.dwVersionMS & 0x0000FFFF,
719 (This->pDesc->vVersion.dwVersionLS >> 8) & 0x0000FFFF, This->pDesc->vVersion.dwVersionLS & 0x0000FFFF);
720 if (This->pDesc->dwValidData & DMUS_OBJ_NAME)
721 TRACE(" - Name = %s\n", debugstr_w(This->pDesc->wszName));
723 TRACE(" - Collection header:\n");
724 TRACE(" - cInstruments: %d\n", This->pHeader->cInstruments);
725 TRACE(" - Instruments:\n");
727 LIST_FOR_EACH (listEntry, &This->Instruments) {
728 tmpEntry = LIST_ENTRY( listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry );
729 TRACE(" - Instrument[%i]: %p\n", r, tmpEntry->pInstrument);
730 r++;
734 return S_OK;
737 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
738 return E_NOTIMPL;
741 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
742 return E_NOTIMPL;
745 static const IPersistStreamVtbl DirectMusicCollection_PersistStream_Vtbl = {
746 IDirectMusicCollectionImpl_IPersistStream_QueryInterface,
747 IDirectMusicCollectionImpl_IPersistStream_AddRef,
748 IDirectMusicCollectionImpl_IPersistStream_Release,
749 IDirectMusicCollectionImpl_IPersistStream_GetClassID,
750 IDirectMusicCollectionImpl_IPersistStream_IsDirty,
751 IDirectMusicCollectionImpl_IPersistStream_Load,
752 IDirectMusicCollectionImpl_IPersistStream_Save,
753 IDirectMusicCollectionImpl_IPersistStream_GetSizeMax
757 /* for ClassFactory */
758 HRESULT WINAPI DMUSIC_CreateDirectMusicCollectionImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
759 IDirectMusicCollectionImpl* obj;
761 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicCollectionImpl));
762 if (NULL == obj) {
763 *ppobj = NULL;
764 return E_OUTOFMEMORY;
766 obj->UnknownVtbl = &DirectMusicCollection_Unknown_Vtbl;
767 obj->CollectionVtbl = &DirectMusicCollection_Collection_Vtbl;
768 obj->ObjectVtbl = &DirectMusicCollection_Object_Vtbl;
769 obj->PersistStreamVtbl = &DirectMusicCollection_PersistStream_Vtbl;
770 obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
771 DM_STRUCT_INIT(obj->pDesc);
772 obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
773 obj->pDesc->guidClass = CLSID_DirectMusicCollection;
774 obj->ref = 0; /* will be inited by QueryInterface */
775 list_init (&obj->Instruments);
777 return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);