c8efcc9ce875b386a7a4968a814620db551a9d88
[reactos.git] / reactos / dll / directx / wine / quartz / enumfilters.c
1 /*
2 * Implementation of IEnumFilters Interface
3 *
4 * Copyright 2004 Christian Costa
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "quartz_private.h"
22
23 typedef struct IEnumFiltersImpl
24 {
25 IEnumFilters IEnumFilters_iface;
26 LONG refCount;
27 IGraphVersion * pVersionSource;
28 LONG Version;
29 IBaseFilter *** pppFilters;
30 ULONG * pNumFilters;
31 ULONG uIndex;
32 } IEnumFiltersImpl;
33
34 static const struct IEnumFiltersVtbl IEnumFiltersImpl_Vtbl;
35
36 static inline IEnumFiltersImpl *impl_from_IEnumFilters(IEnumFilters *iface)
37 {
38 return CONTAINING_RECORD(iface, IEnumFiltersImpl, IEnumFilters_iface);
39 }
40
41 HRESULT IEnumFiltersImpl_Construct(IGraphVersion * pVersionSource, IBaseFilter *** pppFilters, ULONG * pNumFilters, IEnumFilters ** ppEnum)
42 {
43 /* Note: The incoming IBaseFilter interfaces are not AddRef'd here as in Windows,
44 * they should have been previously AddRef'd. */
45 IEnumFiltersImpl * pEnumFilters = CoTaskMemAlloc(sizeof(IEnumFiltersImpl));
46 HRESULT hr;
47 LONG currentVersion;
48
49 TRACE("(%p, %p, %p)\n", pppFilters, pNumFilters, ppEnum);
50
51 *ppEnum = NULL;
52
53 if (!pEnumFilters)
54 {
55 return E_OUTOFMEMORY;
56 }
57
58 pEnumFilters->IEnumFilters_iface.lpVtbl = &IEnumFiltersImpl_Vtbl;
59 pEnumFilters->refCount = 1;
60 pEnumFilters->uIndex = 0;
61 pEnumFilters->pNumFilters = pNumFilters;
62 pEnumFilters->pppFilters = pppFilters;
63 IGraphVersion_AddRef(pVersionSource);
64 pEnumFilters->pVersionSource = pVersionSource;
65
66 /* Store the current version of the graph */
67 hr = IGraphVersion_QueryVersion(pVersionSource, &currentVersion);
68 pEnumFilters->Version = (hr==S_OK) ? currentVersion : 0;
69
70 *ppEnum = &pEnumFilters->IEnumFilters_iface;
71 return S_OK;
72 }
73
74 static HRESULT WINAPI IEnumFiltersImpl_QueryInterface(IEnumFilters * iface, REFIID riid, LPVOID * ppv)
75 {
76 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
77
78 *ppv = NULL;
79
80 if (IsEqualIID(riid, &IID_IUnknown))
81 *ppv = iface;
82 else if (IsEqualIID(riid, &IID_IEnumFilters))
83 *ppv = iface;
84
85 if (*ppv)
86 {
87 IUnknown_AddRef((IUnknown *)(*ppv));
88 return S_OK;
89 }
90
91 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
92
93 return E_NOINTERFACE;
94 }
95
96 static ULONG WINAPI IEnumFiltersImpl_AddRef(IEnumFilters * iface)
97 {
98 IEnumFiltersImpl *This = impl_from_IEnumFilters(iface);
99 ULONG refCount = InterlockedIncrement(&This->refCount);
100
101 TRACE("(%p)->()\n", iface);
102
103 return refCount;
104 }
105
106 static ULONG WINAPI IEnumFiltersImpl_Release(IEnumFilters * iface)
107 {
108 IEnumFiltersImpl *This = impl_from_IEnumFilters(iface);
109 ULONG refCount = InterlockedDecrement(&This->refCount);
110
111 TRACE("(%p)->()\n", iface);
112
113 if (!refCount)
114 {
115 IGraphVersion_Release(This->pVersionSource);
116 return 0;
117 }
118 else
119 return refCount;
120 }
121
122 static HRESULT WINAPI IEnumFiltersImpl_Next(IEnumFilters * iface, ULONG cFilters, IBaseFilter ** ppFilters, ULONG * pcFetched)
123 {
124 ULONG cFetched;
125 ULONG i;
126 LONG currentVersion;
127 IEnumFiltersImpl *This = impl_from_IEnumFilters(iface);
128 HRESULT hr;
129
130 cFetched = min(*This->pNumFilters, This->uIndex + cFilters) - This->uIndex;
131
132 TRACE("(%p)->(%u, %p, %p)\n", iface, cFilters, ppFilters, pcFetched);
133
134 /* First of all check if the graph has changed */
135 hr = IGraphVersion_QueryVersion(This->pVersionSource, &currentVersion);
136 if (hr==S_OK && This->Version != currentVersion)
137 return VFW_E_ENUM_OUT_OF_SYNC;
138
139
140 if (!ppFilters)
141 return E_POINTER;
142
143 for (i = 0; i < cFetched; i++)
144 {
145 ppFilters[i] = (*This->pppFilters)[This->uIndex + i];
146 IBaseFilter_AddRef(ppFilters[i]);
147 }
148
149 This->uIndex += cFetched;
150
151 if (pcFetched)
152 *pcFetched = cFetched;
153
154 if (cFetched != cFilters)
155 return S_FALSE;
156 return S_OK;
157 }
158
159 static HRESULT WINAPI IEnumFiltersImpl_Skip(IEnumFilters * iface, ULONG cFilters)
160 {
161 IEnumFiltersImpl *This = impl_from_IEnumFilters(iface);
162
163 TRACE("(%p)->(%u)\n", iface, cFilters);
164
165 if (This->uIndex + cFilters < *This->pNumFilters)
166 {
167 This->uIndex += cFilters;
168 return S_OK;
169 }
170 return S_FALSE;
171 }
172
173 static HRESULT WINAPI IEnumFiltersImpl_Reset(IEnumFilters * iface)
174 {
175 IEnumFiltersImpl *This = impl_from_IEnumFilters(iface);
176 HRESULT hr;
177 LONG currentVersion;
178
179 TRACE("(%p)->()\n", iface);
180
181 This->uIndex = 0;
182 hr = IGraphVersion_QueryVersion(This->pVersionSource, &currentVersion);
183 if (!hr)
184 This->Version = currentVersion;
185 return S_OK;
186 }
187
188 static HRESULT WINAPI IEnumFiltersImpl_Clone(IEnumFilters * iface, IEnumFilters ** ppEnum)
189 {
190 HRESULT hr;
191 IEnumFiltersImpl *This = impl_from_IEnumFilters(iface);
192
193 TRACE("(%p)->(%p)\n", iface, ppEnum);
194
195 hr = IEnumFiltersImpl_Construct(This->pVersionSource, This->pppFilters, This->pNumFilters, ppEnum);
196 if (FAILED(hr))
197 return hr;
198 return IEnumFilters_Skip(*ppEnum, This->uIndex);
199 }
200
201 static const IEnumFiltersVtbl IEnumFiltersImpl_Vtbl =
202 {
203 IEnumFiltersImpl_QueryInterface,
204 IEnumFiltersImpl_AddRef,
205 IEnumFiltersImpl_Release,
206 IEnumFiltersImpl_Next,
207 IEnumFiltersImpl_Skip,
208 IEnumFiltersImpl_Reset,
209 IEnumFiltersImpl_Clone
210 };