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