amstream: Store parent as struct ddraw_stream in ddraw stream sample.
[wine/zf.git] / dlls / dmime / graph.c
blob85eef9c091db20a3717a56739713c2abefb76828
1 /* IDirectMusicGraph
3 * Copyright (C) 2003-2004 Rok Mandeljc
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "dmime_private.h"
21 #include "dmobject.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(dmime);
25 struct IDirectMusicGraphImpl {
26 IDirectMusicGraph IDirectMusicGraph_iface;
27 struct dmobject dmobj;
28 LONG ref;
29 WORD num_tools;
30 struct list Tools;
33 static inline IDirectMusicGraphImpl *impl_from_IDirectMusicGraph(IDirectMusicGraph *iface)
35 return CONTAINING_RECORD(iface, IDirectMusicGraphImpl, IDirectMusicGraph_iface);
38 static inline IDirectMusicGraphImpl *impl_from_IPersistStream(IPersistStream *iface)
40 return CONTAINING_RECORD(iface, IDirectMusicGraphImpl, dmobj.IPersistStream_iface);
43 static HRESULT WINAPI DirectMusicGraph_QueryInterface(IDirectMusicGraph *iface, REFIID riid, void **ret_iface)
45 IDirectMusicGraphImpl *This = impl_from_IDirectMusicGraph(iface);
47 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ret_iface);
49 *ret_iface = NULL;
51 if (IsEqualIID(riid, &IID_IUnknown) ||
52 IsEqualIID(riid, &IID_IDirectMusicGraph))
54 *ret_iface = &This->IDirectMusicGraph_iface;
56 else if (IsEqualIID(riid, &IID_IDirectMusicObject))
57 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
58 else if (IsEqualIID(riid, &IID_IPersistStream))
59 *ret_iface = &This->dmobj.IPersistStream_iface;
61 if (*ret_iface)
63 IDirectMusicGraph_AddRef(iface);
64 return S_OK;
67 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ret_iface);
68 return E_NOINTERFACE;
71 static ULONG WINAPI DirectMusicGraph_AddRef(IDirectMusicGraph *iface)
73 IDirectMusicGraphImpl *This = impl_from_IDirectMusicGraph(iface);
74 ULONG ref = InterlockedIncrement(&This->ref);
76 TRACE("(%p): %d\n", This, ref);
78 DMIME_LockModule();
79 return ref;
82 static ULONG WINAPI DirectMusicGraph_Release(IDirectMusicGraph *iface)
84 IDirectMusicGraphImpl *This = impl_from_IDirectMusicGraph(iface);
85 ULONG ref = InterlockedDecrement(&This->ref);
87 TRACE("(%p): %d\n", This, ref);
89 if (ref == 0)
90 HeapFree(GetProcessHeap(), 0, This);
92 DMIME_UnlockModule();
93 return ref;
96 static HRESULT WINAPI DirectMusicGraph_StampPMsg(IDirectMusicGraph *iface, DMUS_PMSG *msg)
98 IDirectMusicGraphImpl *This = impl_from_IDirectMusicGraph(iface);
99 FIXME("(%p, %p): stub\n", This, msg);
100 return S_OK;
103 static HRESULT WINAPI DirectMusicGraph_InsertTool(IDirectMusicGraph *iface, IDirectMusicTool* pTool, DWORD* pdwPChannels, DWORD cPChannels, LONG lIndex)
105 IDirectMusicGraphImpl *This = impl_from_IDirectMusicGraph(iface);
106 struct list* pEntry = NULL;
107 struct list* pPrevEntry = NULL;
108 LPDMUS_PRIVATE_GRAPH_TOOL pIt = NULL;
109 LPDMUS_PRIVATE_GRAPH_TOOL pNewTool = NULL;
111 FIXME("(%p, %p, %p, %d, %i): use of pdwPChannels\n", This, pTool, pdwPChannels, cPChannels, lIndex);
113 if (!pTool)
114 return E_POINTER;
116 if (lIndex < 0)
117 lIndex = This->num_tools + lIndex;
119 pPrevEntry = &This->Tools;
120 LIST_FOR_EACH(pEntry, &This->Tools)
122 pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_GRAPH_TOOL, entry);
123 if (pIt->dwIndex == lIndex)
124 return DMUS_E_ALREADY_EXISTS;
126 if (pIt->dwIndex > lIndex)
127 break ;
128 pPrevEntry = pEntry;
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 IDirectMusicTool8_Init(pTool, iface);
137 list_add_tail (pPrevEntry->next, &pNewTool->entry);
139 #if 0
140 DWORD dwNum = 0;
141 IDirectMusicTool8_GetMediaTypes(pTool, &dwNum);
142 #endif
144 return DS_OK;
147 static HRESULT WINAPI DirectMusicGraph_GetTool(IDirectMusicGraph *iface, DWORD dwIndex, IDirectMusicTool** ppTool)
149 IDirectMusicGraphImpl *This = impl_from_IDirectMusicGraph(iface);
150 struct list* pEntry = NULL;
151 LPDMUS_PRIVATE_GRAPH_TOOL pIt = NULL;
153 TRACE("(%p, %d, %p)\n", This, dwIndex, ppTool);
155 LIST_FOR_EACH (pEntry, &This->Tools)
157 pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_GRAPH_TOOL, entry);
158 if (pIt->dwIndex == dwIndex)
160 *ppTool = pIt->pTool;
161 if (*ppTool)
162 IDirectMusicTool_AddRef(*ppTool);
163 return S_OK;
165 if (pIt->dwIndex > dwIndex)
166 break ;
169 return DMUS_E_NOT_FOUND;
172 static HRESULT WINAPI DirectMusicGraph_RemoveTool(IDirectMusicGraph *iface, IDirectMusicTool* pTool)
174 IDirectMusicGraphImpl *This = impl_from_IDirectMusicGraph(iface);
175 FIXME("(%p, %p): stub\n", This, pTool);
176 return S_OK;
179 static const IDirectMusicGraphVtbl DirectMusicGraphVtbl =
181 DirectMusicGraph_QueryInterface,
182 DirectMusicGraph_AddRef,
183 DirectMusicGraph_Release,
184 DirectMusicGraph_StampPMsg,
185 DirectMusicGraph_InsertTool,
186 DirectMusicGraph_GetTool,
187 DirectMusicGraph_RemoveTool
190 static HRESULT WINAPI graph_IDirectMusicObject_ParseDescriptor(IDirectMusicObject *iface,
191 IStream *stream, DMUS_OBJECTDESC *desc)
193 struct chunk_entry riff = {0};
194 HRESULT hr;
196 TRACE("(%p, %p, %p)\n", iface, stream, desc);
198 if (!stream)
199 return E_POINTER;
200 if (!desc || desc->dwSize != sizeof(*desc))
201 return E_INVALIDARG;
203 if ((hr = stream_get_chunk(stream, &riff)) != S_OK)
204 return hr;
205 if (riff.id != FOURCC_RIFF || riff.type != DMUS_FOURCC_TOOLGRAPH_FORM) {
206 TRACE("loading failed: unexpected %s\n", debugstr_chunk(&riff));
207 stream_skip_chunk(stream, &riff);
208 return DMUS_E_CHUNKNOTFOUND;
211 hr = dmobj_parsedescriptor(stream, &riff, desc,
212 DMUS_OBJ_OBJECT|DMUS_OBJ_NAME|DMUS_OBJ_CATEGORY|DMUS_OBJ_VERSION);
213 if (FAILED(hr))
214 return hr;
216 desc->guidClass = CLSID_DirectMusicGraph;
217 desc->dwValidData |= DMUS_OBJ_CLASS;
219 dump_DMUS_OBJECTDESC(desc);
220 return S_OK;
223 static const IDirectMusicObjectVtbl dmobject_vtbl = {
224 dmobj_IDirectMusicObject_QueryInterface,
225 dmobj_IDirectMusicObject_AddRef,
226 dmobj_IDirectMusicObject_Release,
227 dmobj_IDirectMusicObject_GetDescriptor,
228 dmobj_IDirectMusicObject_SetDescriptor,
229 graph_IDirectMusicObject_ParseDescriptor
232 static HRESULT WINAPI graph_IPersistStream_Load(IPersistStream *iface, IStream *stream)
234 IDirectMusicGraphImpl *This = impl_from_IPersistStream(iface);
236 FIXME("(%p, %p): Loading not implemented yet\n", This, stream);
238 return IDirectMusicObject_ParseDescriptor(&This->dmobj.IDirectMusicObject_iface, stream,
239 &This->dmobj.desc);
242 static const IPersistStreamVtbl persiststream_vtbl = {
243 dmobj_IPersistStream_QueryInterface,
244 dmobj_IPersistStream_AddRef,
245 dmobj_IPersistStream_Release,
246 dmobj_IPersistStream_GetClassID,
247 unimpl_IPersistStream_IsDirty,
248 graph_IPersistStream_Load,
249 unimpl_IPersistStream_Save,
250 unimpl_IPersistStream_GetSizeMax
253 /* for ClassFactory */
254 HRESULT WINAPI create_dmgraph(REFIID riid, void **ret_iface)
256 IDirectMusicGraphImpl* obj;
257 HRESULT hr;
259 *ret_iface = NULL;
261 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicGraphImpl));
262 if (!obj)
263 return E_OUTOFMEMORY;
265 obj->IDirectMusicGraph_iface.lpVtbl = &DirectMusicGraphVtbl;
266 obj->ref = 1;
267 dmobject_init(&obj->dmobj, &CLSID_DirectMusicGraph, (IUnknown *)&obj->IDirectMusicGraph_iface);
268 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
269 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
270 list_init(&obj->Tools);
272 hr = IDirectMusicGraph_QueryInterface(&obj->IDirectMusicGraph_iface, riid, ret_iface);
273 IDirectMusicGraph_Release(&obj->IDirectMusicGraph_iface);
275 return hr;