enumfilters.c 5.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Implementation of IEnumFilters Interface
 *
 * Copyright 2004 Christian Costa
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23 24 25 26 27 28
 */

#include "quartz_private.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(quartz);

typedef struct IEnumFiltersImpl
{
29
    IEnumFilters IEnumFilters_iface;
30
    LONG refCount;
31 32 33 34
    IGraphVersion * pVersionSource;
    LONG Version;
    IBaseFilter *** pppFilters;
    ULONG * pNumFilters;
35 36 37 38 39
    ULONG uIndex;
} IEnumFiltersImpl;

static const struct IEnumFiltersVtbl IEnumFiltersImpl_Vtbl;

40 41 42 43 44
static inline IEnumFiltersImpl *impl_from_IEnumFilters(IEnumFilters *iface)
{
    return CONTAINING_RECORD(iface, IEnumFiltersImpl, IEnumFilters_iface);
}

45
HRESULT IEnumFiltersImpl_Construct(IGraphVersion * pVersionSource, IBaseFilter *** pppFilters, ULONG * pNumFilters, IEnumFilters ** ppEnum)
46 47 48 49
{
    /* Note: The incoming IBaseFilter interfaces are not AddRef'd here as in Windows,
     * they should have been previously AddRef'd. */
    IEnumFiltersImpl * pEnumFilters = CoTaskMemAlloc(sizeof(IEnumFiltersImpl));
50 51
    HRESULT hr;
    LONG currentVersion;
52

53
    TRACE("(%p, %p, %p)\n", pppFilters, pNumFilters, ppEnum);
54 55 56 57 58 59 60 61

    *ppEnum = NULL;

    if (!pEnumFilters)
    {
        return E_OUTOFMEMORY;
    }

62
    pEnumFilters->IEnumFilters_iface.lpVtbl = &IEnumFiltersImpl_Vtbl;
63 64
    pEnumFilters->refCount = 1;
    pEnumFilters->uIndex = 0;
65 66 67 68
    pEnumFilters->pNumFilters = pNumFilters;
    pEnumFilters->pppFilters = pppFilters;
    IGraphVersion_AddRef(pVersionSource);
    pEnumFilters->pVersionSource = pVersionSource;
69

70 71 72
    /* Store the current version of the graph */
    hr = IGraphVersion_QueryVersion(pVersionSource, &currentVersion);
    pEnumFilters->Version = (hr==S_OK) ? currentVersion : 0;
73

74
    *ppEnum = &pEnumFilters->IEnumFilters_iface;
75 76 77 78 79 80 81 82 83 84
    return S_OK;
}

static HRESULT WINAPI IEnumFiltersImpl_QueryInterface(IEnumFilters * iface, REFIID riid, LPVOID * ppv)
{
    TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);

    *ppv = NULL;

    if (IsEqualIID(riid, &IID_IUnknown))
85
        *ppv = iface;
86
    else if (IsEqualIID(riid, &IID_IEnumFilters))
87
        *ppv = iface;
88 89 90 91 92 93 94 95 96 97 98 99 100 101

    if (*ppv)
    {
        IUnknown_AddRef((IUnknown *)(*ppv));
        return S_OK;
    }

    FIXME("No interface for %s!\n", qzdebugstr_guid(riid));

    return E_NOINTERFACE;
}

static ULONG WINAPI IEnumFiltersImpl_AddRef(IEnumFilters * iface)
{
102
    IEnumFiltersImpl *This = impl_from_IEnumFilters(iface);
103 104
    ULONG refCount = InterlockedIncrement(&This->refCount);

105
    TRACE("(%p)->()\n", iface);
106 107

    return refCount;
108 109 110 111
}

static ULONG WINAPI IEnumFiltersImpl_Release(IEnumFilters * iface)
{
112
    IEnumFiltersImpl *This = impl_from_IEnumFilters(iface);
113 114
    ULONG refCount = InterlockedDecrement(&This->refCount);

115
    TRACE("(%p)->()\n", iface);
116 117

    if (!refCount)
118
    {
119
        IGraphVersion_Release(This->pVersionSource);
120 121 122
        return 0;
    }
    else
123
        return refCount;
124 125 126 127 128 129
}

static HRESULT WINAPI IEnumFiltersImpl_Next(IEnumFilters * iface, ULONG cFilters, IBaseFilter ** ppFilters, ULONG * pcFetched)
{
    ULONG cFetched; 
    ULONG i;
130
    LONG currentVersion;
131
    IEnumFiltersImpl *This = impl_from_IEnumFilters(iface);
132
    HRESULT hr;
133

134
    cFetched = min(*This->pNumFilters, This->uIndex + cFilters) - This->uIndex;
135

136
    TRACE("(%p)->(%u, %p, %p)\n", iface, cFilters, ppFilters, pcFetched);
137

138 139 140 141 142 143
    /* First of all check if the graph has changed */
    hr = IGraphVersion_QueryVersion(This->pVersionSource, &currentVersion);
    if (hr==S_OK && This->Version != currentVersion)
        return VFW_E_ENUM_OUT_OF_SYNC;


144 145 146
    if (!ppFilters)
        return E_POINTER;

147 148
    for (i = 0; i < cFetched; i++)
    {
149
        ppFilters[i] = (*This->pppFilters)[This->uIndex + i];
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	IBaseFilter_AddRef(ppFilters[i]);
    }

    This->uIndex += cFetched;

    if (pcFetched)
        *pcFetched = cFetched;

    if (cFetched != cFilters)
        return S_FALSE;
    return S_OK;
}

static HRESULT WINAPI IEnumFiltersImpl_Skip(IEnumFilters * iface, ULONG cFilters)
{
165
    IEnumFiltersImpl *This = impl_from_IEnumFilters(iface);
166

167
    TRACE("(%p)->(%u)\n", iface, cFilters);
168

169
    if (This->uIndex + cFilters < *This->pNumFilters)
170 171 172 173 174 175 176 177 178
    {
        This->uIndex += cFilters;
        return S_OK;
    }
    return S_FALSE;
}

static HRESULT WINAPI IEnumFiltersImpl_Reset(IEnumFilters * iface)
{
179
    IEnumFiltersImpl *This = impl_from_IEnumFilters(iface);
180 181
    HRESULT hr;
    LONG currentVersion;
182 183 184 185

    TRACE("(%p)->()\n", iface);

    This->uIndex = 0;
186 187 188
    hr = IGraphVersion_QueryVersion(This->pVersionSource, &currentVersion);
    if (!hr)
        This->Version = currentVersion;
189 190 191 192 193 194
    return S_OK;
}

static HRESULT WINAPI IEnumFiltersImpl_Clone(IEnumFilters * iface, IEnumFilters ** ppEnum)
{
    HRESULT hr;
195
    IEnumFiltersImpl *This = impl_from_IEnumFilters(iface);
196 197 198

    TRACE("(%p)->(%p)\n", iface, ppEnum);

199
    hr = IEnumFiltersImpl_Construct(This->pVersionSource, This->pppFilters, This->pNumFilters, ppEnum);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    if (FAILED(hr))
        return hr;
    return IEnumFilters_Skip(*ppEnum, This->uIndex);
}

static const IEnumFiltersVtbl IEnumFiltersImpl_Vtbl =
{
    IEnumFiltersImpl_QueryInterface,
    IEnumFiltersImpl_AddRef,
    IEnumFiltersImpl_Release,
    IEnumFiltersImpl_Next,
    IEnumFiltersImpl_Skip,
    IEnumFiltersImpl_Reset,
    IEnumFiltersImpl_Clone
};