comctl32/tests: Use CRT allocation functions.
[wine.git] / dlls / dmime / graph.c
blobe0aa94833aeb1c8d555b3ca21643f353caef4ef7
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"
22 WINE_DEFAULT_DEBUG_CHANNEL(dmime);
24 struct tool_entry
26 struct list entry;
27 IDirectMusicTool *tool;
28 DWORD delivery;
31 struct graph
33 IDirectMusicGraph IDirectMusicGraph_iface;
34 struct dmobject dmobj;
35 LONG ref;
37 struct list tools;
40 static inline struct graph *impl_from_IDirectMusicGraph(IDirectMusicGraph *iface)
42 return CONTAINING_RECORD(iface, struct graph, IDirectMusicGraph_iface);
45 static inline struct graph *impl_from_IPersistStream(IPersistStream *iface)
47 return CONTAINING_RECORD(iface, struct graph, dmobj.IPersistStream_iface);
50 static HRESULT WINAPI graph_QueryInterface(IDirectMusicGraph *iface, REFIID riid, void **ret_iface)
52 struct graph *This = impl_from_IDirectMusicGraph(iface);
54 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ret_iface);
56 *ret_iface = NULL;
58 if (IsEqualIID(riid, &IID_IUnknown) ||
59 IsEqualIID(riid, &IID_IDirectMusicGraph))
61 *ret_iface = &This->IDirectMusicGraph_iface;
63 else if (IsEqualIID(riid, &IID_IDirectMusicObject))
64 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
65 else if (IsEqualIID(riid, &IID_IPersistStream))
66 *ret_iface = &This->dmobj.IPersistStream_iface;
68 if (*ret_iface)
70 IDirectMusicGraph_AddRef(iface);
71 return S_OK;
74 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ret_iface);
75 return E_NOINTERFACE;
78 static ULONG WINAPI graph_AddRef(IDirectMusicGraph *iface)
80 struct graph *This = impl_from_IDirectMusicGraph(iface);
81 ULONG ref = InterlockedIncrement(&This->ref);
83 TRACE("(%p): %ld\n", This, ref);
85 return ref;
88 static ULONG WINAPI graph_Release(IDirectMusicGraph *iface)
90 struct graph *This = impl_from_IDirectMusicGraph(iface);
91 ULONG ref = InterlockedDecrement(&This->ref);
93 TRACE("(%p): %ld\n", This, ref);
95 if (!ref)
97 struct tool_entry *entry, *next;
99 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &This->tools, struct tool_entry, entry)
101 list_remove(&entry->entry);
102 IDirectMusicTool_Release(entry->tool);
103 free(entry);
106 free(This);
109 return ref;
112 static HRESULT WINAPI graph_StampPMsg(IDirectMusicGraph *iface, DMUS_PMSG *msg)
114 const DWORD delivery_flags = DMUS_PMSGF_TOOL_IMMEDIATE | DMUS_PMSGF_TOOL_QUEUE | DMUS_PMSGF_TOOL_ATTIME;
115 struct graph *This = impl_from_IDirectMusicGraph(iface);
116 struct tool_entry *entry, *next, *first;
118 TRACE("(%p, %p)\n", This, msg);
120 if (!msg) return E_POINTER;
122 first = LIST_ENTRY(This->tools.next, struct tool_entry, entry);
123 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &This->tools, struct tool_entry, entry)
124 if (entry->tool == msg->pTool) break;
125 if (&entry->entry == &This->tools) next = first;
127 if (msg->pTool)
129 IDirectMusicTool_Release(msg->pTool);
130 msg->pTool = NULL;
133 if (&next->entry == &This->tools) return DMUS_S_LAST_TOOL;
135 if (!msg->pGraph)
137 msg->pGraph = iface;
138 IDirectMusicGraph_AddRef(msg->pGraph);
141 msg->pTool = next->tool;
142 IDirectMusicTool_AddRef(msg->pTool);
144 msg->dwFlags &= ~delivery_flags;
145 msg->dwFlags |= next->delivery;
147 return S_OK;
150 static HRESULT WINAPI graph_InsertTool(IDirectMusicGraph *iface, IDirectMusicTool *tool,
151 DWORD *channels, DWORD channel_count, LONG index)
153 struct graph *This = impl_from_IDirectMusicGraph(iface);
154 struct tool_entry *entry, *next;
155 HRESULT hr;
157 TRACE("(%p, %p, %p, %ld, %li)\n", This, tool, channels, channel_count, index);
159 if (!tool) return E_POINTER;
161 LIST_FOR_EACH_ENTRY(next, &This->tools, struct tool_entry, entry)
163 if (next->tool == tool) return DMUS_E_ALREADY_EXISTS;
164 if (index-- <= 0) break;
167 if (!(entry = calloc(1, sizeof(*entry)))) return E_OUTOFMEMORY;
168 entry->tool = tool;
169 IDirectMusicTool_AddRef(tool);
170 IDirectMusicTool_Init(tool, iface);
171 if (FAILED(hr = IDirectMusicTool_GetMsgDeliveryType(tool, &entry->delivery)))
173 WARN("Failed to get delivery type from tool %p, hr %#lx\n", tool, hr);
174 entry->delivery = DMUS_PMSGF_TOOL_IMMEDIATE;
176 list_add_before(&next->entry, &entry->entry);
178 return S_OK;
181 static HRESULT WINAPI graph_GetTool(IDirectMusicGraph *iface, DWORD index, IDirectMusicTool **ret_tool)
183 struct graph *This = impl_from_IDirectMusicGraph(iface);
184 struct tool_entry *entry;
186 TRACE("(%p, %ld, %p)\n", This, index, ret_tool);
188 if (!ret_tool) return E_POINTER;
190 LIST_FOR_EACH_ENTRY(entry, &This->tools, struct tool_entry, entry)
192 if (!index--)
194 *ret_tool = entry->tool;
195 IDirectMusicTool_AddRef(entry->tool);
196 return S_OK;
200 return DMUS_E_NOT_FOUND;
203 static HRESULT WINAPI graph_RemoveTool(IDirectMusicGraph *iface, IDirectMusicTool *tool)
205 struct graph *This = impl_from_IDirectMusicGraph(iface);
206 struct tool_entry *entry;
208 TRACE("(%p, %p)\n", This, tool);
210 if (!tool) return E_POINTER;
212 LIST_FOR_EACH_ENTRY(entry, &This->tools, struct tool_entry, entry)
214 if (entry->tool == tool)
216 list_remove(&entry->entry);
217 IDirectMusicTool_Release(entry->tool);
218 free(entry);
219 return S_OK;
223 return DMUS_E_NOT_FOUND;
226 static const IDirectMusicGraphVtbl graph_vtbl =
228 graph_QueryInterface,
229 graph_AddRef,
230 graph_Release,
231 graph_StampPMsg,
232 graph_InsertTool,
233 graph_GetTool,
234 graph_RemoveTool,
237 static HRESULT WINAPI graph_IDirectMusicObject_ParseDescriptor(IDirectMusicObject *iface,
238 IStream *stream, DMUS_OBJECTDESC *desc)
240 struct chunk_entry riff = {0};
241 HRESULT hr;
243 TRACE("(%p, %p, %p)\n", iface, stream, desc);
245 if (!stream)
246 return E_POINTER;
247 if (!desc || desc->dwSize != sizeof(*desc))
248 return E_INVALIDARG;
250 if ((hr = stream_get_chunk(stream, &riff)) != S_OK)
251 return hr;
252 if (riff.id != FOURCC_RIFF || riff.type != DMUS_FOURCC_TOOLGRAPH_FORM) {
253 TRACE("loading failed: unexpected %s\n", debugstr_chunk(&riff));
254 stream_skip_chunk(stream, &riff);
255 return DMUS_E_CHUNKNOTFOUND;
258 hr = dmobj_parsedescriptor(stream, &riff, desc,
259 DMUS_OBJ_OBJECT|DMUS_OBJ_NAME|DMUS_OBJ_CATEGORY|DMUS_OBJ_VERSION);
260 if (FAILED(hr))
261 return hr;
263 desc->guidClass = CLSID_DirectMusicGraph;
264 desc->dwValidData |= DMUS_OBJ_CLASS;
266 dump_DMUS_OBJECTDESC(desc);
267 return S_OK;
270 static const IDirectMusicObjectVtbl dmobject_vtbl = {
271 dmobj_IDirectMusicObject_QueryInterface,
272 dmobj_IDirectMusicObject_AddRef,
273 dmobj_IDirectMusicObject_Release,
274 dmobj_IDirectMusicObject_GetDescriptor,
275 dmobj_IDirectMusicObject_SetDescriptor,
276 graph_IDirectMusicObject_ParseDescriptor
279 static HRESULT WINAPI graph_IPersistStream_Load(IPersistStream *iface, IStream *stream)
281 struct graph *This = impl_from_IPersistStream(iface);
283 FIXME("(%p, %p): Loading not implemented yet\n", This, stream);
285 return IDirectMusicObject_ParseDescriptor(&This->dmobj.IDirectMusicObject_iface, stream,
286 &This->dmobj.desc);
289 static const IPersistStreamVtbl persiststream_vtbl = {
290 dmobj_IPersistStream_QueryInterface,
291 dmobj_IPersistStream_AddRef,
292 dmobj_IPersistStream_Release,
293 dmobj_IPersistStream_GetClassID,
294 unimpl_IPersistStream_IsDirty,
295 graph_IPersistStream_Load,
296 unimpl_IPersistStream_Save,
297 unimpl_IPersistStream_GetSizeMax
300 /* for ClassFactory */
301 HRESULT create_dmgraph(REFIID riid, void **ret_iface)
303 struct graph *obj;
304 HRESULT hr;
306 *ret_iface = NULL;
307 if (!(obj = calloc(1, sizeof(*obj)))) return E_OUTOFMEMORY;
308 obj->IDirectMusicGraph_iface.lpVtbl = &graph_vtbl;
309 obj->ref = 1;
310 dmobject_init(&obj->dmobj, &CLSID_DirectMusicGraph, (IUnknown *)&obj->IDirectMusicGraph_iface);
311 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
312 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
313 list_init(&obj->tools);
315 hr = IDirectMusicGraph_QueryInterface(&obj->IDirectMusicGraph_iface, riid, ret_iface);
316 IDirectMusicGraph_Release(&obj->IDirectMusicGraph_iface);
318 return hr;