Properly implement DllCanUnloadNow ref counting.
[wine] / dlls / dmcompos / chordmap.c
1 /* IDirectMusicChordMap Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
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
13  * GNU Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "dmcompos_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(dmcompos);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
24
25 /*****************************************************************************
26  * IDirectMusicChordMapImpl implementation
27  */
28 /* IDirectMusicChordMapImpl IUnknown part: */
29 HRESULT WINAPI IDirectMusicChordMapImpl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
30         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, UnknownVtbl, iface);
31         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
32         
33         if (IsEqualIID (riid, &IID_IUnknown)) {
34                 *ppobj = (LPVOID)&This->UnknownVtbl;
35                 IDirectMusicChordMapImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
36                 return S_OK;    
37         } else if (IsEqualIID (riid, &IID_IDirectMusicChordMap)) {
38                 *ppobj = (LPVOID)&This->ChordMapVtbl;
39                 IDirectMusicChordMapImpl_IDirectMusicChordMap_AddRef ((LPDIRECTMUSICCHORDMAP)&This->ChordMapVtbl);
40                 return S_OK;
41         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
42                 *ppobj = (LPVOID)&This->ObjectVtbl;
43                 IDirectMusicChordMapImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);            
44                 return S_OK;
45         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
46                 *ppobj = (LPVOID)&This->PersistStreamVtbl;
47                 IDirectMusicChordMapImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);             
48                 return S_OK;
49         }
50         
51         WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
52         return E_NOINTERFACE;
53 }
54
55 ULONG WINAPI IDirectMusicChordMapImpl_IUnknown_AddRef (LPUNKNOWN iface) {
56         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, UnknownVtbl, iface);
57         TRACE("(%p): AddRef from %ld\n", This, This->ref);
58         
59         DMCOMPOS_LockModule();
60         
61         return ++(This->ref);
62 }
63
64 ULONG WINAPI IDirectMusicChordMapImpl_IUnknown_Release (LPUNKNOWN iface) {
65         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, UnknownVtbl, iface);
66         ULONG ref = --This->ref;
67         TRACE("(%p): ReleaseRef to %ld\n", This, This->ref);
68         
69         if (ref == 0) {
70                 HeapFree(GetProcessHeap(), 0, This);
71         }
72         
73         DMCOMPOS_UnlockModule();
74         
75         return ref;
76 }
77
78 IUnknownVtbl DirectMusicChordMap_Unknown_Vtbl = {
79         IDirectMusicChordMapImpl_IUnknown_QueryInterface,
80         IDirectMusicChordMapImpl_IUnknown_AddRef,
81         IDirectMusicChordMapImpl_IUnknown_Release
82 };
83
84 /* IDirectMusicChordMapImpl IDirectMusicChordMap part: */
85 HRESULT WINAPI IDirectMusicChordMapImpl_IDirectMusicChordMap_QueryInterface (LPDIRECTMUSICCHORDMAP iface, REFIID riid, LPVOID *ppobj) {
86         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, ChordMapVtbl, iface);
87         return IDirectMusicChordMapImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
88 }
89
90 ULONG WINAPI IDirectMusicChordMapImpl_IDirectMusicChordMap_AddRef (LPDIRECTMUSICCHORDMAP iface) {
91         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, ChordMapVtbl, iface);
92         return IDirectMusicChordMapImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
93 }
94
95 ULONG WINAPI IDirectMusicChordMapImpl_IDirectMusicChordMap_Release (LPDIRECTMUSICCHORDMAP iface) {
96         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, ChordMapVtbl, iface);
97         return IDirectMusicChordMapImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
98 }
99
100 HRESULT WINAPI IDirectMusicChordMapImpl_IDirectMusicChordMap_GetScale (LPDIRECTMUSICCHORDMAP iface, DWORD* pdwScale) {
101         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, ChordMapVtbl, iface);
102         FIXME("(%p, %p): stub\n", This, pdwScale);
103         return S_OK;
104 }
105
106 IDirectMusicChordMapVtbl DirectMusicChordMap_ChordMap_Vtbl = {
107         IDirectMusicChordMapImpl_IDirectMusicChordMap_QueryInterface,
108         IDirectMusicChordMapImpl_IDirectMusicChordMap_AddRef,
109         IDirectMusicChordMapImpl_IDirectMusicChordMap_Release,
110         IDirectMusicChordMapImpl_IDirectMusicChordMap_GetScale
111 };
112
113 /* IDirectMusicChordMapImpl IDirectMusicObject part: */
114 HRESULT WINAPI IDirectMusicChordMapImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
115         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, ObjectVtbl, iface);
116         return IDirectMusicChordMapImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
117 }
118
119 ULONG WINAPI IDirectMusicChordMapImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
120         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, ObjectVtbl, iface);
121         return IDirectMusicChordMapImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
122 }
123
124 ULONG WINAPI IDirectMusicChordMapImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
125         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, ObjectVtbl, iface);
126         return IDirectMusicChordMapImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
127 }
128
129 HRESULT WINAPI IDirectMusicChordMapImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
130         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, ObjectVtbl, iface);
131         TRACE("(%p, %p)\n", This, pDesc);
132         /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
133         memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
134         return S_OK;
135 }
136
137 HRESULT WINAPI IDirectMusicChordMapImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
138         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, ObjectVtbl, iface);
139         TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
140         
141         /* According to MSDN, we should copy only given values, not whole struct */     
142         if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
143                 memcpy (&This->pDesc->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
144         if (pDesc->dwValidData & DMUS_OBJ_CLASS)
145                 memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (pDesc->guidClass));         
146         if (pDesc->dwValidData & DMUS_OBJ_NAME)
147                 strncpyW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
148         if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
149                 strncpyW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);             
150         if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
151                 strncpyW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);             
152         if (pDesc->dwValidData & DMUS_OBJ_VERSION)
153                 memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));                            
154         if (pDesc->dwValidData & DMUS_OBJ_DATE)
155                 memcpy (&This->pDesc->ftDate, &pDesc->ftDate, sizeof (pDesc->ftDate));                          
156         if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
157                 memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));                           
158                 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
159         }
160         if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
161                 /* according to MSDN, we copy the stream */
162                 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);  
163         }
164         
165         /* add new flags */
166         This->pDesc->dwValidData |= pDesc->dwValidData;
167
168         return S_OK;
169 }
170
171 HRESULT WINAPI IDirectMusicChordMapImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
172         DMUS_PRIVATE_CHUNK Chunk;
173         DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
174         LARGE_INTEGER liMove; /* used when skipping chunks */
175
176         TRACE("(%p, %p)\n", pStream, pDesc);
177         
178         /* FIXME: should this be determined from stream? */
179         pDesc->dwValidData |= DMUS_OBJ_CLASS;
180         memcpy (&pDesc->guidClass, &CLSID_DirectMusicChordMap, sizeof(CLSID));
181         
182         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
183         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
184         switch (Chunk.fccID) {  
185                 case FOURCC_RIFF: {
186                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
187                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
188                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
189                         StreamCount = 0;
190                         if (Chunk.fccID == DMUS_FOURCC_CHORDMAP_FORM) {
191                                 TRACE_(dmfile)(": chord map form\n");
192                                 do {
193                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
194                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
195                                         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
196                                         switch (Chunk.fccID) {
197                                                 case DMUS_FOURCC_GUID_CHUNK: {
198                                                         TRACE_(dmfile)(": GUID chunk\n");
199                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
200                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
201                                                         break;
202                                                 }
203                                                 case DMUS_FOURCC_VERSION_CHUNK: {
204                                                         TRACE_(dmfile)(": version chunk\n");
205                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
206                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
207                                                         break;
208                                                 }
209                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
210                                                         TRACE_(dmfile)(": category chunk\n");
211                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
212                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
213                                                         break;
214                                                 }
215                                                 case FOURCC_LIST: {
216                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
217                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
218                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
219                                                         ListCount[0] = 0;
220                                                         switch (Chunk.fccID) {
221                                                                 /* evil M$ UNFO list, which can (!?) contain INFO elements */
222                                                                 case DMUS_FOURCC_UNFO_LIST: {
223                                                                         TRACE_(dmfile)(": UNFO list\n");
224                                                                         do {
225                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
226                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
227                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
228                                                                                 switch (Chunk.fccID) {
229                                                                                         /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
230                                              (though strings seem to be valid unicode) */
231                                                                                         case mmioFOURCC('I','N','A','M'):
232                                                                                         case DMUS_FOURCC_UNAM_CHUNK: {
233                                                                                                 TRACE_(dmfile)(": name chunk\n");
234                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
235                                                                                                 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
236                                                                                                 break;
237                                                                                         }
238                                                                                         case mmioFOURCC('I','A','R','T'):
239                                                                                         case DMUS_FOURCC_UART_CHUNK: {
240                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
241                                                                                                 liMove.QuadPart = Chunk.dwSize;
242                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
243                                                                                                 break;
244                                                                                         }
245                                                                                         case mmioFOURCC('I','C','O','P'):
246                                                                                         case DMUS_FOURCC_UCOP_CHUNK: {
247                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
248                                                                                                 liMove.QuadPart = Chunk.dwSize;
249                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
250                                                                                                 break;
251                                                                                         }
252                                                                                         case mmioFOURCC('I','S','B','J'):
253                                                                                         case DMUS_FOURCC_USBJ_CHUNK: {
254                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
255                                                                                                 liMove.QuadPart = Chunk.dwSize;
256                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
257                                                                                                 break;
258                                                                                         }
259                                                                                         case mmioFOURCC('I','C','M','T'):
260                                                                                         case DMUS_FOURCC_UCMT_CHUNK: {
261                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
262                                                                                                 liMove.QuadPart = Chunk.dwSize;
263                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
264                                                                                                 break;
265                                                                                         }
266                                                                                         default: {
267                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
268                                                                                                 liMove.QuadPart = Chunk.dwSize;
269                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
270                                                                                                 break;                                          
271                                                                                         }
272                                                                                 }
273                                                                                 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
274                                                                         } while (ListCount[0] < ListSize[0]);
275                                                                         break;
276                                                                 }
277                                                                 default: {
278                                                                         TRACE_(dmfile)(": unknown (skipping)\n");
279                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
280                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
281                                                                         break;                                          
282                                                                 }
283                                                         }
284                                                         break;
285                                                 }       
286                                                 default: {
287                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
288                                                         liMove.QuadPart = Chunk.dwSize;
289                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
290                                                         break;                                          
291                                                 }
292                                         }
293                                         TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
294                                 } while (StreamCount < StreamSize);
295                                 break;
296                         } else {
297                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
298                                 liMove.QuadPart = StreamSize;
299                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
300                                 return E_FAIL;
301                         }
302                 
303                         TRACE_(dmfile)(": reading finished\n");
304                         break;
305                 }
306                 default: {
307                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
308                         liMove.QuadPart = Chunk.dwSize;
309                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
310                         return DMUS_E_INVALIDFILE;
311                 }
312         }       
313         
314         TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
315         
316         return S_OK;
317 }
318
319 IDirectMusicObjectVtbl DirectMusicChordMap_Object_Vtbl = {
320         IDirectMusicChordMapImpl_IDirectMusicObject_QueryInterface,
321         IDirectMusicChordMapImpl_IDirectMusicObject_AddRef,
322         IDirectMusicChordMapImpl_IDirectMusicObject_Release,
323         IDirectMusicChordMapImpl_IDirectMusicObject_GetDescriptor,
324         IDirectMusicChordMapImpl_IDirectMusicObject_SetDescriptor,
325         IDirectMusicChordMapImpl_IDirectMusicObject_ParseDescriptor
326 };
327
328         
329 /* IDirectMusicChordMapImpl IPersistStream part: */
330 HRESULT WINAPI IDirectMusicChordMapImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
331         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, PersistStreamVtbl, iface);
332         return IDirectMusicChordMapImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
333 }
334
335 ULONG WINAPI IDirectMusicChordMapImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
336         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, PersistStreamVtbl, iface);
337         return IDirectMusicChordMapImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
338 }
339
340 ULONG WINAPI IDirectMusicChordMapImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
341         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, PersistStreamVtbl, iface);
342         return IDirectMusicChordMapImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
343 }
344
345 HRESULT WINAPI IDirectMusicChordMapImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
346         return E_NOTIMPL;
347 }
348
349 HRESULT WINAPI IDirectMusicChordMapImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
350         return E_NOTIMPL;
351 }
352
353 HRESULT WINAPI IDirectMusicChordMapImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
354         ICOM_THIS_MULTI(IDirectMusicChordMapImpl, PersistStreamVtbl, iface);
355
356         FOURCC chunkID;
357         DWORD chunkSize, StreamSize, StreamCount, ListSize[3], ListCount[3];
358         LARGE_INTEGER liMove; /* used when skipping chunks */
359
360         FIXME("(%p, %p): Loading not implemented yet\n", This, pStm);
361         IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
362         IStream_Read (pStm, &chunkSize, sizeof(DWORD), NULL);
363         TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (chunkID), chunkSize);
364         switch (chunkID) {      
365                 case FOURCC_RIFF: {
366                         IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);                            
367                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(chunkID));
368                         StreamSize = chunkSize - sizeof(FOURCC);
369                         StreamCount = 0;
370                         switch (chunkID) {
371                                 case DMUS_FOURCC_CHORDMAP_FORM: {
372                                         TRACE_(dmfile)(": chordmap form\n");
373                                         do {
374                                                 IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
375                                                 IStream_Read (pStm, &chunkSize, sizeof(FOURCC), NULL);
376                                                 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + chunkSize;
377                                                 TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (chunkID), chunkSize);
378                                                 switch (chunkID) {
379                                                         case DMUS_FOURCC_GUID_CHUNK: {
380                                                                 TRACE_(dmfile)(": GUID chunk\n");
381                                                                 This->pDesc->dwValidData |= DMUS_OBJ_OBJECT;
382                                                                 IStream_Read (pStm, &This->pDesc->guidObject, chunkSize, NULL);
383                                                                 break;
384                                                         }
385                                                         case DMUS_FOURCC_VERSION_CHUNK: {
386                                                                 TRACE_(dmfile)(": version chunk\n");
387                                                                 This->pDesc->dwValidData |= DMUS_OBJ_VERSION;
388                                                                 IStream_Read (pStm, &This->pDesc->vVersion, chunkSize, NULL);
389                                                                 break;
390                                                         }
391                                                         case DMUS_FOURCC_CATEGORY_CHUNK: {
392                                                                 TRACE_(dmfile)(": category chunk\n");
393                                                                 This->pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
394                                                                 IStream_Read (pStm, This->pDesc->wszCategory, chunkSize, NULL);
395                                                                 break;
396                                                         }
397                                                         case FOURCC_LIST: {
398                                                                 IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);                            
399                                                                 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(chunkID));
400                                                                 ListSize[0] = chunkSize - sizeof(FOURCC);
401                                                                 ListCount[0] = 0;
402                                                                 switch (chunkID) {
403                                                                         case DMUS_FOURCC_UNFO_LIST: {
404                                                                                 TRACE_(dmfile)(": UNFO list\n");
405                                                                                 do {
406                                                                                         IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
407                                                                                         IStream_Read (pStm, &chunkSize, sizeof(FOURCC), NULL);
408                                                                                         ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + chunkSize;
409                                                                                         TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (chunkID), chunkSize);
410                                                                                         switch (chunkID) {
411                                                                                                 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
412                                               (though strings seem to be valid unicode) */
413                                                                                                 case mmioFOURCC('I','N','A','M'):
414                                                                                                 case DMUS_FOURCC_UNAM_CHUNK: {
415                                                                                                         TRACE_(dmfile)(": name chunk\n");
416                                                                                                         This->pDesc->dwValidData |= DMUS_OBJ_NAME;
417                                                                                                         IStream_Read (pStm, This->pDesc->wszName, chunkSize, NULL);
418                                                                                                         break;
419                                                                                                 }
420                                                                                                 case mmioFOURCC('I','A','R','T'):
421                                                                                                 case DMUS_FOURCC_UART_CHUNK: {
422                                                                                                         TRACE_(dmfile)(": artist chunk (ignored)\n");
423                                                                                                         liMove.QuadPart = chunkSize;
424                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
425                                                                                                         break;
426                                                                                                 }
427                                                                                                 case mmioFOURCC('I','C','O','P'):
428                                                                                                 case DMUS_FOURCC_UCOP_CHUNK: {
429                                                                                                         TRACE_(dmfile)(": copyright chunk (ignored)\n");
430                                                                                                         liMove.QuadPart = chunkSize;
431                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
432                                                                                                         break;
433                                                                                                 }
434                                                                                                 case mmioFOURCC('I','S','B','J'):
435                                                                                                 case DMUS_FOURCC_USBJ_CHUNK: {
436                                                                                                         TRACE_(dmfile)(": subject chunk (ignored)\n");
437                                                                                                         liMove.QuadPart = chunkSize;
438                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
439                                                                                                         break;
440                                                                                                 }
441                                                                                                 case mmioFOURCC('I','C','M','T'):
442                                                                                                 case DMUS_FOURCC_UCMT_CHUNK: {
443                                                                                                         TRACE_(dmfile)(": comment chunk (ignored)\n");
444                                                                                                         liMove.QuadPart = chunkSize;
445                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
446                                                                                                         break;
447                                                                                                 }
448                                                                                                 default: {
449                                                                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
450                                                                                                         liMove.QuadPart = chunkSize;
451                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
452                                                                                                         break;                                          
453                                                                                                 }
454                                                                                         }
455                                                                                         TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
456                                                                                 } while (ListCount[0] < ListSize[0]);
457                                                                                 break;
458                                                                         }
459                                                                         default: {
460                                                                                 TRACE_(dmfile)(": unknown (skipping)\n");
461                                                                                 liMove.QuadPart = chunkSize - sizeof(FOURCC);
462                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
463                                                                                 break;                                          
464                                                                         }
465                                                                 }
466                                                                 break;
467                                                         }       
468                                                         default: {
469                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
470                                                                 liMove.QuadPart = chunkSize;
471                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
472                                                                 break;                                          
473                                                         }
474                                                 }
475                                                 TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
476                                         } while (StreamCount < StreamSize);
477                                         break;
478                                 }
479                                 default: {
480                                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
481                                         liMove.QuadPart = StreamSize;
482                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
483                                         return E_FAIL;
484                                 }
485                         }
486                         TRACE_(dmfile)(": reading finished\n");
487                         break;
488                 }
489                 default: {
490                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
491                         liMove.QuadPart = chunkSize;
492                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
493                         return E_FAIL;
494                 }
495         }
496
497         return S_OK;
498 }
499
500 HRESULT WINAPI IDirectMusicChordMapImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
501         return E_NOTIMPL;
502 }
503
504 HRESULT WINAPI IDirectMusicChordMapImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
505         return E_NOTIMPL;
506 }
507
508 IPersistStreamVtbl DirectMusicChordMap_PersistStream_Vtbl = {
509         IDirectMusicChordMapImpl_IPersistStream_QueryInterface,
510         IDirectMusicChordMapImpl_IPersistStream_AddRef,
511         IDirectMusicChordMapImpl_IPersistStream_Release,
512         IDirectMusicChordMapImpl_IPersistStream_GetClassID,
513         IDirectMusicChordMapImpl_IPersistStream_IsDirty,
514         IDirectMusicChordMapImpl_IPersistStream_Load,
515         IDirectMusicChordMapImpl_IPersistStream_Save,
516         IDirectMusicChordMapImpl_IPersistStream_GetSizeMax
517 };
518
519 /* for ClassFactory */
520 HRESULT WINAPI DMUSIC_CreateDirectMusicChordMapImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
521         IDirectMusicChordMapImpl* obj;
522         
523         obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicChordMapImpl));
524         if (NULL == obj) {
525                 *ppobj = (LPVOID) NULL;
526                 return E_OUTOFMEMORY;
527         }
528         obj->UnknownVtbl = &DirectMusicChordMap_Unknown_Vtbl;
529         obj->ChordMapVtbl = &DirectMusicChordMap_ChordMap_Vtbl;
530         obj->ObjectVtbl = &DirectMusicChordMap_Object_Vtbl;
531         obj->PersistStreamVtbl = &DirectMusicChordMap_PersistStream_Vtbl;
532         obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
533         DM_STRUCT_INIT(obj->pDesc);
534         obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
535         memcpy (&obj->pDesc->guidClass, &CLSID_DirectMusicChordMap, sizeof (CLSID));
536         obj->ref = 0; /* will be inited by QueryInterface */
537         
538         return IDirectMusicChordMapImpl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
539 }