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