mf/samplegrabber: Handle paused state.
[wine.git] / dlls / windowscodecs / propertybag.c
blob71b26309140ed21277408c366cb83b0f3d9aca61
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2013 Ludger Sprenker
5 * This library 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 library 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 library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "objbase.h"
28 #include "wincodecs_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
34 typedef struct PropertyBag {
35 IPropertyBag2 IPropertyBag2_iface;
36 LONG ref;
37 UINT prop_count;
38 PROPBAG2 *properties;
39 VARIANT *values;
40 } PropertyBag;
42 static inline PropertyBag *impl_from_IPropertyBag2(IPropertyBag2 *iface)
44 return CONTAINING_RECORD(iface, PropertyBag, IPropertyBag2_iface);
47 static HRESULT WINAPI PropertyBag_QueryInterface(IPropertyBag2 *iface, REFIID iid,
48 void **ppv)
50 PropertyBag *This = impl_from_IPropertyBag2(iface);
51 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
53 if (!ppv) return E_INVALIDARG;
55 if (IsEqualIID(&IID_IUnknown, iid) ||
56 IsEqualIID(&IID_IPropertyBag2, iid))
58 *ppv = &This->IPropertyBag2_iface;
60 else
62 *ppv = NULL;
63 return E_NOINTERFACE;
66 IUnknown_AddRef((IUnknown*)*ppv);
67 return S_OK;
70 static ULONG WINAPI PropertyBag_AddRef(IPropertyBag2 *iface)
72 PropertyBag *This = impl_from_IPropertyBag2(iface);
73 ULONG ref = InterlockedIncrement(&This->ref);
75 TRACE("(%p) refcount=%u\n", iface, ref);
77 return ref;
80 static ULONG WINAPI PropertyBag_Release(IPropertyBag2 *iface)
82 PropertyBag *This = impl_from_IPropertyBag2(iface);
83 ULONG ref = InterlockedDecrement(&This->ref);
85 TRACE("(%p) refcount=%u\n", iface, ref);
87 if (ref == 0)
89 ULONG i;
90 if (This->properties && This->values)
92 for (i=0; i < This->prop_count; i++)
94 CoTaskMemFree(This->properties[i].pstrName);
95 VariantClear( This->values+i );
99 HeapFree(GetProcessHeap(), 0, This->properties);
100 HeapFree(GetProcessHeap(), 0, This->values);
101 HeapFree(GetProcessHeap(), 0, This);
104 return ref;
107 static LONG find_item(PropertyBag *This, LPCOLESTR name)
109 LONG i;
110 if (!This->properties)
111 return -1;
112 if (!name)
113 return -1;
115 for (i=0; i < This->prop_count; i++)
117 if (wcscmp(name, This->properties[i].pstrName) == 0)
118 return i;
121 return -1;
124 static HRESULT WINAPI PropertyBag_Read(IPropertyBag2 *iface, ULONG cProperties,
125 PROPBAG2 *pPropBag, IErrorLog *pErrLog, VARIANT *pvarValue, HRESULT *phrError)
127 HRESULT res = S_OK;
128 ULONG i;
129 PropertyBag *This = impl_from_IPropertyBag2(iface);
131 TRACE("(%p,%u,%p,%p,%p,%p)\n", iface, cProperties, pPropBag, pErrLog, pvarValue, phrError);
133 for (i=0; i < cProperties; i++)
135 LONG idx;
136 if (pPropBag[i].dwHint && pPropBag[i].dwHint <= This->prop_count)
137 idx = pPropBag[i].dwHint-1;
138 else
139 idx = find_item(This, pPropBag[i].pstrName);
141 if (idx > -1)
143 VariantInit(pvarValue+i);
144 res = VariantCopy(pvarValue+i, This->values+idx);
145 if (FAILED(res))
146 break;
147 phrError[i] = res;
149 else
151 res = E_FAIL;
152 break;
156 return res;
159 static HRESULT WINAPI PropertyBag_Write(IPropertyBag2 *iface, ULONG cProperties,
160 PROPBAG2 *pPropBag, VARIANT *pvarValue)
162 HRESULT res = S_OK;
163 ULONG i;
164 PropertyBag *This = impl_from_IPropertyBag2(iface);
166 TRACE("(%p,%u,%p,%p)\n", iface, cProperties, pPropBag, pvarValue);
168 for (i=0; i < cProperties; i++)
170 LONG idx;
171 if (pPropBag[i].dwHint && pPropBag[i].dwHint <= This->prop_count)
172 idx = pPropBag[i].dwHint-1;
173 else
174 idx = find_item(This, pPropBag[i].pstrName);
176 if (idx > -1)
178 if (This->properties[idx].vt != V_VT(pvarValue+i))
179 return WINCODEC_ERR_PROPERTYUNEXPECTEDTYPE;
180 res = VariantCopy(This->values+idx, pvarValue+i);
181 if (FAILED(res))
182 return E_FAIL;
184 else
186 if (pPropBag[i].pstrName)
187 FIXME("Application tried to set the unknown option %s.\n",
188 debugstr_w(pPropBag[i].pstrName));
190 /* FIXME: Function is not atomar on error, but MSDN does not say anything about it
191 * (no reset of items between 0 and i-1) */
192 return E_FAIL;
196 return res;
199 static HRESULT WINAPI PropertyBag_CountProperties(IPropertyBag2 *iface, ULONG *pcProperties)
201 PropertyBag *This = impl_from_IPropertyBag2(iface);
203 TRACE("(%p,%p)\n", iface, pcProperties);
205 if (!pcProperties)
206 return E_INVALIDARG;
208 *pcProperties = This->prop_count;
210 return S_OK;
213 static HRESULT copy_propbag2(PROPBAG2 *dest, const PROPBAG2 *src)
215 dest->cfType = src->cfType;
216 dest->clsid = src->clsid;
217 dest->dwHint = src->dwHint;
218 dest->dwType = src->dwType;
219 dest->vt = src->vt;
220 dest->pstrName = CoTaskMemAlloc((lstrlenW(src->pstrName)+1) * sizeof(WCHAR));
221 if(!dest->pstrName)
222 return E_OUTOFMEMORY;
224 lstrcpyW(dest->pstrName, src->pstrName);
226 return S_OK;
229 static HRESULT WINAPI PropertyBag_GetPropertyInfo(IPropertyBag2 *iface, ULONG iProperty,
230 ULONG cProperties, PROPBAG2 *pPropBag, ULONG *pcProperties)
232 HRESULT res = S_OK;
233 ULONG i;
234 PropertyBag *This = impl_from_IPropertyBag2(iface);
236 TRACE("(%p,%u,%u,%p,%p)\n", iface, iProperty, cProperties, pPropBag, pcProperties);
238 if (iProperty >= This->prop_count && iProperty > 0)
239 return WINCODEC_ERR_VALUEOUTOFRANGE;
240 if (iProperty+cProperties > This->prop_count )
241 return WINCODEC_ERR_VALUEOUTOFRANGE;
243 *pcProperties = min(cProperties, This->prop_count-iProperty);
245 for (i=0; i < *pcProperties; i++)
247 res = copy_propbag2(pPropBag+i, This->properties+iProperty+i);
248 if (FAILED(res))
250 do {
251 CoTaskMemFree( pPropBag[--i].pstrName );
252 } while (i);
253 break;
257 return res;
260 static HRESULT WINAPI PropertyBag_LoadObject(IPropertyBag2 *iface, LPCOLESTR pstrName,
261 DWORD dwHint, IUnknown *pUnkObject, IErrorLog *pErrLog)
263 FIXME("(%p,%s,%u,%p,%p): stub\n", iface, debugstr_w(pstrName), dwHint, pUnkObject, pErrLog);
264 return E_NOTIMPL;
267 static const IPropertyBag2Vtbl PropertyBag_Vtbl = {
268 PropertyBag_QueryInterface,
269 PropertyBag_AddRef,
270 PropertyBag_Release,
271 PropertyBag_Read,
272 PropertyBag_Write,
273 PropertyBag_CountProperties,
274 PropertyBag_GetPropertyInfo,
275 PropertyBag_LoadObject
278 HRESULT CreatePropertyBag2(const PROPBAG2 *options, UINT count,
279 IPropertyBag2 **ppPropertyBag2)
281 UINT i;
282 HRESULT res = S_OK;
283 PropertyBag *This;
285 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PropertyBag));
286 if (!This) return E_OUTOFMEMORY;
288 This->IPropertyBag2_iface.lpVtbl = &PropertyBag_Vtbl;
289 This->ref = 1;
290 This->prop_count = count;
292 if (count == 0)
294 This->properties = NULL;
295 This->values = NULL;
297 else
299 This->properties = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PROPBAG2)*count);
300 This->values = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(VARIANT)*count);
302 if (!This->properties || !This->values)
303 res = E_OUTOFMEMORY;
304 else
305 for (i=0; i < count; i++)
307 res = copy_propbag2(This->properties+i, options+i);
308 if (FAILED(res))
309 break;
310 This->properties[i].dwHint = i+1; /* 0 means unset, so we start with 1 */
314 if (FAILED(res))
316 PropertyBag_Release(&This->IPropertyBag2_iface);
317 *ppPropertyBag2 = NULL;
319 else
320 *ppPropertyBag2 = &This->IPropertyBag2_iface;
322 return res;