quartz: Fix time discontinuities in the acm wrapper.
[wine] / dlls / quartz / enumpins.c
1 /*
2  * Implementation of IEnumPins Interface
3  *
4  * Copyright 2003 Robert Shearman
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 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
26
27 typedef struct IEnumPinsImpl
28 {
29     const IEnumPinsVtbl * lpVtbl;
30     LONG refCount;
31     ENUMPINDETAILS enumPinDetails;
32     ULONG uIndex;
33 } IEnumPinsImpl;
34
35 static const struct IEnumPinsVtbl IEnumPinsImpl_Vtbl;
36
37 HRESULT IEnumPinsImpl_Construct(const ENUMPINDETAILS * pDetails, IEnumPins ** ppEnum)
38 {
39     IEnumPinsImpl * pEnumPins;
40
41     if (!ppEnum)
42         return E_POINTER;
43
44     pEnumPins = CoTaskMemAlloc(sizeof(IEnumPinsImpl));
45     if (!pEnumPins)
46     {
47         *ppEnum = NULL;
48         return E_OUTOFMEMORY;
49     }
50     pEnumPins->lpVtbl = &IEnumPinsImpl_Vtbl;
51     pEnumPins->refCount = 1;
52     pEnumPins->uIndex = 0;
53     pEnumPins->enumPinDetails = *pDetails;
54     *ppEnum = (IEnumPins *)(&pEnumPins->lpVtbl);
55
56     TRACE("Created new enumerator (%p)\n", *ppEnum);
57     return S_OK;
58 }
59
60 static HRESULT WINAPI IEnumPinsImpl_QueryInterface(IEnumPins * iface, REFIID riid, LPVOID * ppv)
61 {
62     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
63
64     *ppv = NULL;
65
66     if (IsEqualIID(riid, &IID_IUnknown))
67         *ppv = (LPVOID)iface;
68     else if (IsEqualIID(riid, &IID_IEnumPins))
69         *ppv = (LPVOID)iface;
70
71     if (*ppv)
72     {
73         IUnknown_AddRef((IUnknown *)(*ppv));
74         return S_OK;
75     }
76
77     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
78
79     return E_NOINTERFACE;
80 }
81
82 static ULONG WINAPI IEnumPinsImpl_AddRef(IEnumPins * iface)
83 {
84     IEnumPinsImpl *This = (IEnumPinsImpl *)iface;
85     ULONG refCount = InterlockedIncrement(&This->refCount);
86
87     TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
88
89     return refCount;
90 }
91
92 static ULONG WINAPI IEnumPinsImpl_Release(IEnumPins * iface)
93 {
94     IEnumPinsImpl *This = (IEnumPinsImpl *)iface;
95     ULONG refCount = InterlockedDecrement(&This->refCount);
96
97     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
98     
99     if (!refCount)
100     {
101         CoTaskMemFree(This);
102         return 0;
103     }
104     else
105         return refCount;
106 }
107
108 static HRESULT WINAPI IEnumPinsImpl_Next(IEnumPins * iface, ULONG cPins, IPin ** ppPins, ULONG * pcFetched)
109 {
110     ULONG cFetched; 
111     IEnumPinsImpl *This = (IEnumPinsImpl *)iface;
112
113     cFetched = min(This->enumPinDetails.cPins, This->uIndex + cPins) - This->uIndex;
114
115     TRACE("(%u, %p, %p)\n", cPins, ppPins, pcFetched);
116
117     if (!ppPins)
118         return E_POINTER;
119
120     if (cPins > 1 && !pcFetched)
121         return E_INVALIDARG;
122
123     if (cFetched > 0)
124     {
125         ULONG i;
126         for (i = 0; i < cFetched; i++) {
127             IPin_AddRef(This->enumPinDetails.ppPins[This->uIndex + i]);
128             ppPins[i] = This->enumPinDetails.ppPins[This->uIndex + i];
129         }
130     }
131
132     if ((cPins != 1) || pcFetched)
133         *pcFetched = cFetched;
134
135     This->uIndex += cFetched;
136
137     if (cFetched != cPins)
138         return S_FALSE;
139     return S_OK;
140 }
141
142 static HRESULT WINAPI IEnumPinsImpl_Skip(IEnumPins * iface, ULONG cPins)
143 {
144     IEnumPinsImpl *This = (IEnumPinsImpl *)iface;
145
146     TRACE("(%u)\n", cPins);
147
148     if (This->uIndex + cPins < This->enumPinDetails.cPins)
149     {
150         This->uIndex += cPins;
151         return S_OK;
152     }
153     return S_FALSE;
154 }
155
156 static HRESULT WINAPI IEnumPinsImpl_Reset(IEnumPins * iface)
157 {
158     IEnumPinsImpl *This = (IEnumPinsImpl *)iface;
159
160     TRACE("IEnumPinsImpl::Reset()\n");
161
162     This->uIndex = 0;
163     return S_OK;
164 }
165
166 static HRESULT WINAPI IEnumPinsImpl_Clone(IEnumPins * iface, IEnumPins ** ppEnum)
167 {
168     HRESULT hr;
169     IEnumPinsImpl *This = (IEnumPinsImpl *)iface;
170
171     TRACE("(%p)\n", ppEnum);
172
173     hr = IEnumPinsImpl_Construct(&This->enumPinDetails, ppEnum);
174     if (FAILED(hr))
175         return hr;
176     return IEnumPins_Skip(*ppEnum, This->uIndex);
177 }
178
179 static const IEnumPinsVtbl IEnumPinsImpl_Vtbl =
180 {
181     IEnumPinsImpl_QueryInterface,
182     IEnumPinsImpl_AddRef,
183     IEnumPinsImpl_Release,
184     IEnumPinsImpl_Next,
185     IEnumPinsImpl_Skip,
186     IEnumPinsImpl_Reset,
187     IEnumPinsImpl_Clone
188 };