Remove unneeded NULL casts.
[wine] / dlls / dmime / graph.c
1 /* IDirectMusicGraph
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 "dmime_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(dmime);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
24
25 /*****************************************************************************
26  * IDirectMusicGraphImpl implementation
27  */
28 /* IDirectMusicGraphImpl IUnknown part: */
29 HRESULT WINAPI IDirectMusicGraphImpl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
30         ICOM_THIS_MULTI(IDirectMusicGraphImpl, UnknownVtbl, iface);
31         TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
32         
33         if (IsEqualIID (riid, &IID_IUnknown)) {
34                 *ppobj = (LPVOID)&This->UnknownVtbl;
35                 IDirectMusicGraphImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
36                 return S_OK;    
37         } else if (IsEqualIID (riid, &IID_IDirectMusicGraph)) {
38                 *ppobj = (LPVOID)&This->GraphVtbl;
39                 IDirectMusicGraphImpl_IDirectMusicGraph_AddRef ((LPDIRECTMUSICGRAPH)&This->GraphVtbl);
40                 return S_OK;
41         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
42                 *ppobj = (LPVOID)&This->ObjectVtbl;
43                 IDirectMusicGraphImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);               
44                 return S_OK;
45         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
46                 *ppobj = (LPVOID)&This->PersistStreamVtbl;
47                 IDirectMusicGraphImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);                
48                 return S_OK;
49         }
50         
51         WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
52         return E_NOINTERFACE;
53 }
54
55 ULONG WINAPI IDirectMusicGraphImpl_IUnknown_AddRef (LPUNKNOWN iface) {
56         ICOM_THIS_MULTI(IDirectMusicGraphImpl, UnknownVtbl, iface);
57         TRACE("(%p): AddRef from %ld\n", This, This->ref);
58         return ++(This->ref);
59 }
60
61 ULONG WINAPI IDirectMusicGraphImpl_IUnknown_Release (LPUNKNOWN iface) {
62         ICOM_THIS_MULTI(IDirectMusicGraphImpl, UnknownVtbl, iface);
63         ULONG ref = --This->ref;
64         TRACE("(%p): ReleaseRef to %ld\n", This, This->ref);
65         if (ref == 0) {
66                 HeapFree(GetProcessHeap(), 0, This);
67         }
68         return ref;
69 }
70
71 IUnknownVtbl DirectMusicGraph_Unknown_Vtbl = {
72         IDirectMusicGraphImpl_IUnknown_QueryInterface,
73         IDirectMusicGraphImpl_IUnknown_AddRef,
74         IDirectMusicGraphImpl_IUnknown_Release
75 };
76
77 /* IDirectMusicGraphImpl IDirectMusicGraph part: */
78 HRESULT WINAPI IDirectMusicGraphImpl_IDirectMusicGraph_QueryInterface (LPDIRECTMUSICGRAPH iface, REFIID riid, LPVOID *ppobj) {
79         ICOM_THIS_MULTI(IDirectMusicGraphImpl, GraphVtbl, iface);
80         return IDirectMusicGraphImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
81 }
82
83 ULONG WINAPI IDirectMusicGraphImpl_IDirectMusicGraph_AddRef (LPDIRECTMUSICGRAPH iface) {
84         ICOM_THIS_MULTI(IDirectMusicGraphImpl, GraphVtbl, iface);
85         return IDirectMusicGraphImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
86 }
87
88 ULONG WINAPI IDirectMusicGraphImpl_IDirectMusicGraph_Release (LPDIRECTMUSICGRAPH iface) {
89         ICOM_THIS_MULTI(IDirectMusicGraphImpl, GraphVtbl, iface);
90         return IDirectMusicGraphImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
91 }
92
93 HRESULT WINAPI IDirectMusicGraphImpl_IDirectMusicGraph_StampPMsg (LPDIRECTMUSICGRAPH iface, DMUS_PMSG* pPMSG) {
94         ICOM_THIS_MULTI(IDirectMusicGraphImpl, GraphVtbl, iface);
95         FIXME("(%p, %p): stub\n", This, pPMSG);
96         return S_OK;
97 }
98
99 HRESULT WINAPI IDirectMusicGraphImpl_IDirectMusicGraph_InsertTool (LPDIRECTMUSICGRAPH iface, IDirectMusicTool* pTool, DWORD* pdwPChannels, DWORD cPChannels, LONG lIndex) {
100   ICOM_THIS_MULTI(IDirectMusicGraphImpl, GraphVtbl, iface);
101
102   HRESULT hr; 
103   struct list* pEntry = NULL;
104   struct list* pPrevEntry = NULL;
105   LPDMUS_PRIVATE_GRAPH_TOOL pIt = NULL;
106   LPDMUS_PRIVATE_GRAPH_TOOL pNewTool = NULL;
107
108   
109   FIXME("(%p, %p, %p, %ld, %li): use of pdwPChannels\n", This, pTool, pdwPChannels, cPChannels, lIndex);
110   
111   if (NULL == pTool) {
112     return E_POINTER;
113   }
114
115   if (0 > lIndex) {
116     lIndex = This->num_tools + lIndex;
117   }
118
119   pPrevEntry = &This->Tools;
120   LIST_FOR_EACH (pEntry, &This->Tools) {
121     pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_GRAPH_TOOL, entry);
122     if (pIt->dwIndex == lIndex) {
123       return DMUS_E_ALREADY_EXISTS;
124     }
125     if (pIt->dwIndex > lIndex) {
126       break ;
127     }
128     pPrevEntry = pEntry;
129   }
130
131   ++This->num_tools;
132   pNewTool = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_GRAPH_TOOL));
133   pNewTool->pTool = pTool;
134   pNewTool->dwIndex = lIndex;
135   IDirectMusicTool8_AddRef(pTool);
136   hr = IDirectMusicTool8_Init(pTool, iface);
137   list_add_tail (pPrevEntry->next, &pNewTool->entry);
138
139 #if 0
140   DWORD dwNum = 0;
141   hr = IDirectMusicTool8_GetMediaTypes(pTool, &dwNum);
142 #endif
143  
144   return DS_OK;
145 }
146
147 HRESULT WINAPI IDirectMusicGraphImpl_IDirectMusicGraph_GetTool (LPDIRECTMUSICGRAPH iface, DWORD dwIndex, IDirectMusicTool** ppTool) {
148   ICOM_THIS_MULTI(IDirectMusicGraphImpl, GraphVtbl, iface);
149   struct list* pEntry = NULL;
150   LPDMUS_PRIVATE_GRAPH_TOOL pIt = NULL;
151   
152   FIXME("(%p, %ld, %p): stub\n", This, dwIndex, ppTool);
153
154   LIST_FOR_EACH (pEntry, &This->Tools) {
155     pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_GRAPH_TOOL, entry);
156     if (pIt->dwIndex == dwIndex) {
157       *ppTool = pIt->pTool;
158       if (NULL != *ppTool) {
159         IDirectMusicTool8Impl_AddRef((LPDIRECTMUSICTOOL8) *ppTool);
160       }
161       return S_OK;      
162     }
163     if (pIt->dwIndex > dwIndex) {
164       break ;
165     }
166   }
167
168   return DMUS_E_NOT_FOUND;
169 }
170
171 HRESULT WINAPI IDirectMusicGraphImpl_IDirectMusicGraph_RemoveTool (LPDIRECTMUSICGRAPH iface, IDirectMusicTool* pTool) {
172   ICOM_THIS_MULTI(IDirectMusicGraphImpl, GraphVtbl, iface);
173   FIXME("(%p, %p): stub\n", This, pTool);
174   return S_OK;
175 }
176
177 IDirectMusicGraphVtbl DirectMusicGraph_Graph_Vtbl = {
178   IDirectMusicGraphImpl_IDirectMusicGraph_QueryInterface,
179   IDirectMusicGraphImpl_IDirectMusicGraph_AddRef,
180   IDirectMusicGraphImpl_IDirectMusicGraph_Release,
181   IDirectMusicGraphImpl_IDirectMusicGraph_StampPMsg,
182   IDirectMusicGraphImpl_IDirectMusicGraph_InsertTool,
183   IDirectMusicGraphImpl_IDirectMusicGraph_GetTool,
184   IDirectMusicGraphImpl_IDirectMusicGraph_RemoveTool
185 };
186
187
188 /* IDirectMusicGraphImpl IDirectMusicObject part: */
189 HRESULT WINAPI IDirectMusicGraphImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
190         ICOM_THIS_MULTI(IDirectMusicGraphImpl, ObjectVtbl, iface);
191         return IDirectMusicGraphImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
192 }
193
194 ULONG WINAPI IDirectMusicGraphImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
195         ICOM_THIS_MULTI(IDirectMusicGraphImpl, ObjectVtbl, iface);
196         return IDirectMusicGraphImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
197 }
198
199 ULONG WINAPI IDirectMusicGraphImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
200         ICOM_THIS_MULTI(IDirectMusicGraphImpl, ObjectVtbl, iface);
201         return IDirectMusicGraphImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
202 }
203
204 HRESULT WINAPI IDirectMusicGraphImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
205         ICOM_THIS_MULTI(IDirectMusicGraphImpl, ObjectVtbl, iface);
206         TRACE("(%p, %p)\n", This, pDesc);
207         /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
208         memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
209         return S_OK;
210 }
211
212 HRESULT WINAPI IDirectMusicGraphImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
213         ICOM_THIS_MULTI(IDirectMusicGraphImpl, ObjectVtbl, iface);
214         TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
215         
216         /* According to MSDN, we should copy only given values, not whole struct */     
217         if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
218                 memcpy (&This->pDesc->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
219         if (pDesc->dwValidData & DMUS_OBJ_CLASS)
220                 memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (pDesc->guidClass));         
221         if (pDesc->dwValidData & DMUS_OBJ_NAME)
222                 strncpyW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
223         if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
224                 strncpyW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);             
225         if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
226                 strncpyW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);             
227         if (pDesc->dwValidData & DMUS_OBJ_VERSION)
228                 memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));                            
229         if (pDesc->dwValidData & DMUS_OBJ_DATE)
230                 memcpy (&This->pDesc->ftDate, &pDesc->ftDate, sizeof (pDesc->ftDate));                          
231         if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
232                 memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));                           
233                 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
234         }
235         if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
236                 /* according to MSDN, we copy the stream */
237                 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);  
238         }
239         
240         /* add new flags */
241         This->pDesc->dwValidData |= pDesc->dwValidData;
242
243         return S_OK;
244 }
245
246 HRESULT WINAPI IDirectMusicGraphImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
247         ICOM_THIS_MULTI(IDirectMusicGraphImpl, ObjectVtbl, iface);
248         DMUS_PRIVATE_CHUNK Chunk;
249         DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
250         LARGE_INTEGER liMove; /* used when skipping chunks */
251
252         TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
253         
254         /* FIXME: should this be determined from stream? */
255         pDesc->dwValidData |= DMUS_OBJ_CLASS;
256         memcpy (&pDesc->guidClass, &CLSID_DirectMusicGraph, sizeof(CLSID));
257         
258         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
259         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
260         switch (Chunk.fccID) {  
261                 case FOURCC_RIFF: {
262                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
263                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
264                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
265                         StreamCount = 0;
266                         if (Chunk.fccID == DMUS_FOURCC_TOOLGRAPH_FORM) {
267                                 TRACE_(dmfile)(": graph form\n");
268                                 do {
269                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
270                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
271                                         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
272                                         switch (Chunk.fccID) {
273                                                 case DMUS_FOURCC_GUID_CHUNK: {
274                                                         TRACE_(dmfile)(": GUID chunk\n");
275                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
276                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
277                                                         break;
278                                                 }
279                                                 case DMUS_FOURCC_VERSION_CHUNK: {
280                                                         TRACE_(dmfile)(": version chunk\n");
281                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
282                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
283                                                         break;
284                                                 }
285                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
286                                                         TRACE_(dmfile)(": category chunk\n");
287                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
288                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
289                                                         break;
290                                                 }
291                                                 case FOURCC_LIST: {
292                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
293                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
294                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
295                                                         ListCount[0] = 0;
296                                                         switch (Chunk.fccID) {
297                                                                 /* evil M$ UNFO list, which can (!?) contain INFO elements */
298                                                                 case DMUS_FOURCC_UNFO_LIST: {
299                                                                         TRACE_(dmfile)(": UNFO list\n");
300                                                                         do {
301                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
302                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
303                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
304                                                                                 switch (Chunk.fccID) {
305                                                                                         /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
306                                              (though strings seem to be valid unicode) */
307                                                                                         case mmioFOURCC('I','N','A','M'):
308                                                                                         case DMUS_FOURCC_UNAM_CHUNK: {
309                                                                                                 TRACE_(dmfile)(": name chunk\n");
310                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
311                                                                                                 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
312                                                                                                 break;
313                                                                                         }
314                                                                                         case mmioFOURCC('I','A','R','T'):
315                                                                                         case DMUS_FOURCC_UART_CHUNK: {
316                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
317                                                                                                 liMove.QuadPart = Chunk.dwSize;
318                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
319                                                                                                 break;
320                                                                                         }
321                                                                                         case mmioFOURCC('I','C','O','P'):
322                                                                                         case DMUS_FOURCC_UCOP_CHUNK: {
323                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
324                                                                                                 liMove.QuadPart = Chunk.dwSize;
325                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
326                                                                                                 break;
327                                                                                         }
328                                                                                         case mmioFOURCC('I','S','B','J'):
329                                                                                         case DMUS_FOURCC_USBJ_CHUNK: {
330                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
331                                                                                                 liMove.QuadPart = Chunk.dwSize;
332                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
333                                                                                                 break;
334                                                                                         }
335                                                                                         case mmioFOURCC('I','C','M','T'):
336                                                                                         case DMUS_FOURCC_UCMT_CHUNK: {
337                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
338                                                                                                 liMove.QuadPart = Chunk.dwSize;
339                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
340                                                                                                 break;
341                                                                                         }
342                                                                                         default: {
343                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
344                                                                                                 liMove.QuadPart = Chunk.dwSize;
345                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
346                                                                                                 break;                                          
347                                                                                         }
348                                                                                 }
349                                                                                 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
350                                                                         } while (ListCount[0] < ListSize[0]);
351                                                                         break;
352                                                                 }
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;                                          
358                                                                 }
359                                                         }
360                                                         break;
361                                                 }       
362                                                 default: {
363                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
364                                                         liMove.QuadPart = Chunk.dwSize;
365                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
366                                                         break;                                          
367                                                 }
368                                         }
369                                         TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
370                                 } while (StreamCount < StreamSize);
371                                 break;
372                         } else {
373                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
374                                 liMove.QuadPart = StreamSize;
375                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
376                                 return E_FAIL;
377                         }
378                 
379                         TRACE_(dmfile)(": reading finished\n");
380                         break;
381                 }
382                 default: {
383                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
384                         liMove.QuadPart = Chunk.dwSize;
385                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
386                         return DMUS_E_INVALIDFILE;
387                 }
388         }       
389         
390         TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
391         
392         return S_OK;
393 }
394
395 IDirectMusicObjectVtbl DirectMusicGraph_Object_Vtbl = {
396         IDirectMusicGraphImpl_IDirectMusicObject_QueryInterface,
397         IDirectMusicGraphImpl_IDirectMusicObject_AddRef,
398         IDirectMusicGraphImpl_IDirectMusicObject_Release,
399         IDirectMusicGraphImpl_IDirectMusicObject_GetDescriptor,
400         IDirectMusicGraphImpl_IDirectMusicObject_SetDescriptor,
401         IDirectMusicGraphImpl_IDirectMusicObject_ParseDescriptor
402 };
403
404 /* IDirectMusicGraphImpl IPersistStream part: */
405 HRESULT WINAPI IDirectMusicGraphImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
406         ICOM_THIS_MULTI(IDirectMusicGraphImpl, PersistStreamVtbl, iface);
407         return IDirectMusicGraphImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
408 }
409
410 ULONG WINAPI IDirectMusicGraphImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
411         ICOM_THIS_MULTI(IDirectMusicGraphImpl, PersistStreamVtbl, iface);
412         return IDirectMusicGraphImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
413 }
414
415 ULONG WINAPI IDirectMusicGraphImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
416         ICOM_THIS_MULTI(IDirectMusicGraphImpl, PersistStreamVtbl, iface);
417         return IDirectMusicGraphImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
418 }
419
420 HRESULT WINAPI IDirectMusicGraphImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
421         return E_NOTIMPL;
422 }
423
424 HRESULT WINAPI IDirectMusicGraphImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
425         return E_NOTIMPL;
426 }
427
428 HRESULT WINAPI IDirectMusicGraphImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
429         ICOM_THIS_MULTI(IDirectMusicGraphImpl, PersistStreamVtbl, iface);
430         FOURCC chunkID;
431         DWORD chunkSize, StreamSize, StreamCount, ListSize[3], ListCount[3];
432         LARGE_INTEGER liMove; /* used when skipping chunks */
433
434         FIXME("(%p, %p): Loading not implemented yet\n", This, pStm);
435         IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
436         IStream_Read (pStm, &chunkSize, sizeof(DWORD), NULL);
437         TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (chunkID), chunkSize);
438         switch (chunkID) {      
439                 case FOURCC_RIFF: {
440                         IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);                            
441                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(chunkID));
442                         StreamSize = chunkSize - sizeof(FOURCC);
443                         StreamCount = 0;
444                         switch (chunkID) {
445                                 case DMUS_FOURCC_TOOLGRAPH_FORM: {
446                                         TRACE_(dmfile)(": graph form\n");
447                                         do {
448                                                 IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
449                                                 IStream_Read (pStm, &chunkSize, sizeof(FOURCC), NULL);
450                                                 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + chunkSize;
451                                                 TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (chunkID), chunkSize);
452                                                 switch (chunkID) {
453                                                         case DMUS_FOURCC_GUID_CHUNK: {
454                                                                 TRACE_(dmfile)(": GUID chunk\n");
455                                                                 This->pDesc->dwValidData |= DMUS_OBJ_OBJECT;
456                                                                 IStream_Read (pStm, &This->pDesc->guidObject, chunkSize, NULL);
457                                                                 break;
458                                                         }
459                                                         case DMUS_FOURCC_VERSION_CHUNK: {
460                                                                 TRACE_(dmfile)(": version chunk\n");
461                                                                 This->pDesc->dwValidData |= DMUS_OBJ_VERSION;
462                                                                 IStream_Read (pStm, &This->pDesc->vVersion, chunkSize, NULL);
463                                                                 break;
464                                                         }
465                                                         case DMUS_FOURCC_CATEGORY_CHUNK: {
466                                                                 TRACE_(dmfile)(": category chunk\n");
467                                                                 This->pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
468                                                                 IStream_Read (pStm, This->pDesc->wszCategory, chunkSize, NULL);
469                                                                 break;
470                                                         }
471                                                         case FOURCC_LIST: {
472                                                                 IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);                            
473                                                                 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(chunkID));
474                                                                 ListSize[0] = chunkSize - sizeof(FOURCC);
475                                                                 ListCount[0] = 0;
476                                                                 switch (chunkID) {
477                                                                         case DMUS_FOURCC_UNFO_LIST: {
478                                                                                 TRACE_(dmfile)(": UNFO list\n");
479                                                                                 do {
480                                                                                         IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
481                                                                                         IStream_Read (pStm, &chunkSize, sizeof(FOURCC), NULL);
482                                                                                         ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + chunkSize;
483                                                                                         TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (chunkID), chunkSize);
484                                                                                         switch (chunkID) {
485                                                                                                 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
486                                               (though strings seem to be valid unicode) */
487                                                                                                 case mmioFOURCC('I','N','A','M'):
488                                                                                                 case DMUS_FOURCC_UNAM_CHUNK: {
489                                                                                                         TRACE_(dmfile)(": name chunk\n");
490                                                                                                         This->pDesc->dwValidData |= DMUS_OBJ_NAME;
491                                                                                                         IStream_Read (pStm, This->pDesc->wszName, chunkSize, NULL);
492                                                                                                         break;
493                                                                                                 }
494                                                                                                 case mmioFOURCC('I','A','R','T'):
495                                                                                                 case DMUS_FOURCC_UART_CHUNK: {
496                                                                                                         TRACE_(dmfile)(": artist chunk (ignored)\n");
497                                                                                                         liMove.QuadPart = chunkSize;
498                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
499                                                                                                         break;
500                                                                                                 }
501                                                                                                 case mmioFOURCC('I','C','O','P'):
502                                                                                                 case DMUS_FOURCC_UCOP_CHUNK: {
503                                                                                                         TRACE_(dmfile)(": copyright chunk (ignored)\n");
504                                                                                                         liMove.QuadPart = chunkSize;
505                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
506                                                                                                         break;
507                                                                                                 }
508                                                                                                 case mmioFOURCC('I','S','B','J'):
509                                                                                                 case DMUS_FOURCC_USBJ_CHUNK: {
510                                                                                                         TRACE_(dmfile)(": subject chunk (ignored)\n");
511                                                                                                         liMove.QuadPart = chunkSize;
512                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
513                                                                                                         break;
514                                                                                                 }
515                                                                                                 case mmioFOURCC('I','C','M','T'):
516                                                                                                 case DMUS_FOURCC_UCMT_CHUNK: {
517                                                                                                         TRACE_(dmfile)(": comment chunk (ignored)\n");
518                                                                                                         liMove.QuadPart = chunkSize;
519                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
520                                                                                                         break;
521                                                                                                 }
522                                                                                                 default: {
523                                                                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
524                                                                                                         liMove.QuadPart = chunkSize;
525                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
526                                                                                                         break;                                          
527                                                                                                 }
528                                                                                         }
529                                                                                         TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
530                                                                                 } while (ListCount[0] < ListSize[0]);
531                                                                                 break;
532                                                                         }
533                                                                         default: {
534                                                                                 TRACE_(dmfile)(": unknown (skipping)\n");
535                                                                                 liMove.QuadPart = chunkSize - sizeof(FOURCC);
536                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
537                                                                                 break;                                          
538                                                                         }
539                                                                 }
540                                                                 break;
541                                                         }       
542                                                         default: {
543                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
544                                                                 liMove.QuadPart = chunkSize;
545                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
546                                                                 break;                                          
547                                                         }
548                                                 }
549                                                 TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
550                                         } while (StreamCount < StreamSize);
551                                         break;
552                                 }
553                                 default: {
554                                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
555                                         liMove.QuadPart = StreamSize;
556                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
557                                         return E_FAIL;
558                                 }
559                         }
560                         TRACE_(dmfile)(": reading finished\n");
561                         break;
562                 }
563                 default: {
564                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
565                         liMove.QuadPart = chunkSize;
566                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
567                         return E_FAIL;
568                 }
569         }
570
571         return S_OK;
572 }
573
574 HRESULT WINAPI IDirectMusicGraphImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
575         return E_NOTIMPL;
576 }
577
578 HRESULT WINAPI IDirectMusicGraphImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
579         return E_NOTIMPL;
580 }
581
582 IPersistStreamVtbl DirectMusicGraph_PersistStream_Vtbl = {
583         IDirectMusicGraphImpl_IPersistStream_QueryInterface,
584         IDirectMusicGraphImpl_IPersistStream_AddRef,
585         IDirectMusicGraphImpl_IPersistStream_Release,
586         IDirectMusicGraphImpl_IPersistStream_GetClassID,
587         IDirectMusicGraphImpl_IPersistStream_IsDirty,
588         IDirectMusicGraphImpl_IPersistStream_Load,
589         IDirectMusicGraphImpl_IPersistStream_Save,
590         IDirectMusicGraphImpl_IPersistStream_GetSizeMax
591 };
592
593 /* for ClassFactory */
594 HRESULT WINAPI DMUSIC_CreateDirectMusicGraphImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
595   IDirectMusicGraphImpl* obj;
596   
597   obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicGraphImpl));
598   if (NULL == obj) {
599     *ppobj = NULL;
600     return E_OUTOFMEMORY;
601   }
602   obj->UnknownVtbl = &DirectMusicGraph_Unknown_Vtbl;
603   obj->GraphVtbl = &DirectMusicGraph_Graph_Vtbl;
604   obj->ObjectVtbl = &DirectMusicGraph_Object_Vtbl;
605   obj->PersistStreamVtbl = &DirectMusicGraph_PersistStream_Vtbl;
606   obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
607   DM_STRUCT_INIT(obj->pDesc);
608   obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
609   memcpy (&obj->pDesc->guidClass, &CLSID_DirectMusicGraph, sizeof (CLSID));
610   obj->ref = 0; /* will be inited by QueryInterface */
611   list_init (&obj->Tools);
612   
613   return IDirectMusicGraphImpl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
614 }