imm32: Retrieve the graphics driver module from gdi32.
[wine] / dlls / windowscodecs / propertybag.c
1 /*
2  * Copyright 2009 Vincent Povirk for CodeWeavers
3  * Copyright 2013 Ludger Sprenker
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "config.h"
21
22 #include <stdarg.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "objbase.h"
29 #include "wincodec.h"
30 #include "wine/unicode.h"
31
32 #include "wincodecs_private.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
37
38 typedef struct PropertyBag {
39     IPropertyBag2 IPropertyBag2_iface;
40     LONG ref;
41     UINT prop_count;
42     PROPBAG2 *properties;
43     VARIANT *values;
44 } PropertyBag;
45
46 static inline PropertyBag *impl_from_IPropertyBag2(IPropertyBag2 *iface)
47 {
48     return CONTAINING_RECORD(iface, PropertyBag, IPropertyBag2_iface);
49 }
50
51 static HRESULT WINAPI PropertyBag_QueryInterface(IPropertyBag2 *iface, REFIID iid,
52     void **ppv)
53 {
54     PropertyBag *This = impl_from_IPropertyBag2(iface);
55     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
56
57     if (!ppv) return E_INVALIDARG;
58
59     if (IsEqualIID(&IID_IUnknown, iid) ||
60         IsEqualIID(&IID_IPropertyBag2, iid))
61     {
62         *ppv = &This->IPropertyBag2_iface;
63     }
64     else
65     {
66         *ppv = NULL;
67         return E_NOINTERFACE;
68     }
69
70     IUnknown_AddRef((IUnknown*)*ppv);
71     return S_OK;
72 }
73
74 static ULONG WINAPI PropertyBag_AddRef(IPropertyBag2 *iface)
75 {
76     PropertyBag *This = impl_from_IPropertyBag2(iface);
77     ULONG ref = InterlockedIncrement(&This->ref);
78
79     TRACE("(%p) refcount=%u\n", iface, ref);
80
81     return ref;
82 }
83
84 static ULONG WINAPI PropertyBag_Release(IPropertyBag2 *iface)
85 {
86     PropertyBag *This = impl_from_IPropertyBag2(iface);
87     ULONG ref = InterlockedDecrement(&This->ref);
88
89     TRACE("(%p) refcount=%u\n", iface, ref);
90
91     if (ref == 0)
92     {
93         ULONG i;
94         if (This->properties && This->values)
95         {
96             for (i=0; i < This->prop_count; i++)
97             {
98                 HeapFree(GetProcessHeap(), 0, This->properties[i].pstrName);
99                 VariantClear( This->values+i );
100             }
101         }
102
103         HeapFree(GetProcessHeap(), 0, This->properties);
104         HeapFree(GetProcessHeap(), 0, This->values);
105         HeapFree(GetProcessHeap(), 0, This);
106     }
107
108     return ref;
109 }
110
111 static LONG find_item(PropertyBag *This, LPCOLESTR name)
112 {
113     LONG i;
114     if (!This->properties)
115         return -1;
116     if (!name)
117         return -1;
118
119     for (i=0; i < This->prop_count; i++)
120     {
121         if (strcmpW(name, This->properties[i].pstrName) == 0)
122             return i;
123     }
124
125     return -1;
126 }
127
128 static HRESULT WINAPI PropertyBag_Read(IPropertyBag2 *iface, ULONG cProperties,
129     PROPBAG2 *pPropBag, IErrorLog *pErrLog, VARIANT *pvarValue, HRESULT *phrError)
130 {
131     HRESULT res = S_OK;
132     ULONG i;
133     PropertyBag *This = impl_from_IPropertyBag2(iface);
134
135     TRACE("(%p,%u,%p,%p,%p,%p)\n", iface, cProperties, pPropBag, pErrLog, pvarValue, phrError);
136
137     for (i=0; i < cProperties; i++)
138     {
139         LONG idx;
140         if (pPropBag[i].dwHint && pPropBag[i].dwHint <= This->prop_count)
141             idx = pPropBag[i].dwHint-1;
142         else
143             idx = find_item(This, pPropBag[i].pstrName);
144
145         if (idx > -1)
146         {
147             VariantInit(pvarValue+i);
148             res = VariantCopy(pvarValue+i, This->values+idx);
149             if (FAILED(res))
150                 break;
151             phrError[i] = res;
152         }
153         else
154         {
155             res = E_FAIL;
156             break;
157         }
158     }
159
160     return res;
161 }
162
163 static HRESULT WINAPI PropertyBag_Write(IPropertyBag2 *iface, ULONG cProperties,
164     PROPBAG2 *pPropBag, VARIANT *pvarValue)
165 {
166     HRESULT res = S_OK;
167     ULONG i;
168     PropertyBag *This = impl_from_IPropertyBag2(iface);
169
170     TRACE("(%p,%u,%p,%p)\n", iface, cProperties, pPropBag, pvarValue);
171
172     for (i=0; i < cProperties; i++)
173     {
174         LONG idx;
175         if (pPropBag[i].dwHint && pPropBag[i].dwHint <= This->prop_count)
176             idx = pPropBag[i].dwHint-1;
177         else
178             idx = find_item(This, pPropBag[i].pstrName);
179
180         if (idx > -1)
181         {
182             if (This->properties[idx].vt != V_VT(pvarValue+i))
183                 return WINCODEC_ERR_PROPERTYUNEXPECTEDTYPE;
184             res = VariantCopy(This->values+idx, pvarValue+i);
185             if (FAILED(res))
186                 return E_FAIL;
187         }
188         else
189         {
190             if (pPropBag[i].pstrName)
191                 FIXME("Application tried to set the unknown option %s.\n",
192                       debugstr_w(pPropBag[i].pstrName));
193
194             /* FIXME: Function is not atomar on error, but MSDN does not say anything about it
195              *        (no reset of items between 0 and i-1) */
196             return E_FAIL;
197         }
198     }
199
200     return res;
201 }
202
203 static HRESULT WINAPI PropertyBag_CountProperties(IPropertyBag2 *iface, ULONG *pcProperties)
204 {
205     PropertyBag *This = impl_from_IPropertyBag2(iface);
206
207     TRACE("(%p,%p)\n", iface, pcProperties);
208
209     if (!pcProperties)
210         return E_INVALIDARG;
211
212     *pcProperties = This->prop_count;
213
214     return S_OK;
215 }
216
217 static HRESULT copy_propbag2(PROPBAG2 *dest, PROPBAG2 *src, BOOL useCoAlloc)
218 {
219     dest->cfType = src->cfType;
220     dest->clsid = src->clsid;
221     dest->dwHint = src->dwHint;
222     dest->dwType = src->dwType;
223     dest->vt = src->vt;
224     if(useCoAlloc)
225         dest->pstrName = CoTaskMemAlloc((strlenW(src->pstrName)+1) * sizeof(WCHAR));
226     else
227         dest->pstrName = HeapAlloc(GetProcessHeap(), 0, (strlenW(src->pstrName)+1) * sizeof(WCHAR));
228
229     if(!dest->pstrName)
230         return E_OUTOFMEMORY;
231
232     strcpyW(dest->pstrName, src->pstrName);
233
234     return S_OK;
235 }
236
237 static HRESULT WINAPI PropertyBag_GetPropertyInfo(IPropertyBag2 *iface, ULONG iProperty,
238     ULONG cProperties, PROPBAG2 *pPropBag, ULONG *pcProperties)
239 {
240     HRESULT res = S_OK;
241     ULONG i;
242     PropertyBag *This = impl_from_IPropertyBag2(iface);
243
244     TRACE("(%p,%u,%u,%p,%p)\n", iface, iProperty, cProperties, pPropBag, pcProperties);
245
246     if (iProperty >= This->prop_count && iProperty > 0)
247         return WINCODEC_ERR_VALUEOUTOFRANGE;
248     if (iProperty+cProperties > This->prop_count )
249         return WINCODEC_ERR_VALUEOUTOFRANGE;
250
251     *pcProperties = max(cProperties, This->prop_count-iProperty);
252
253     for (i=0; i < *pcProperties; i++)
254     {
255         res = copy_propbag2(pPropBag+i, This->properties+iProperty+i, TRUE);
256         if (FAILED(res))
257         {
258             do {
259                 CoTaskMemFree( pPropBag[--i].pstrName );
260             } while (i);
261             break;
262         }
263     }
264
265     return res;
266 }
267
268 static HRESULT WINAPI PropertyBag_LoadObject(IPropertyBag2 *iface, LPCOLESTR pstrName,
269     DWORD dwHint, IUnknown *pUnkObject, IErrorLog *pErrLog)
270 {
271     FIXME("(%p,%s,%u,%p,%p): stub\n", iface, debugstr_w(pstrName), dwHint, pUnkObject, pErrLog);
272     return E_NOTIMPL;
273 }
274
275 static const IPropertyBag2Vtbl PropertyBag_Vtbl = {
276     PropertyBag_QueryInterface,
277     PropertyBag_AddRef,
278     PropertyBag_Release,
279     PropertyBag_Read,
280     PropertyBag_Write,
281     PropertyBag_CountProperties,
282     PropertyBag_GetPropertyInfo,
283     PropertyBag_LoadObject
284 };
285
286 HRESULT CreatePropertyBag2(PROPBAG2 *options, UINT count,
287                            IPropertyBag2 **ppPropertyBag2)
288 {
289     UINT i;
290     HRESULT res = S_OK;
291     PropertyBag *This;
292
293     This = HeapAlloc(GetProcessHeap(), 0, sizeof(PropertyBag));
294     if (!This) return E_OUTOFMEMORY;
295
296     This->IPropertyBag2_iface.lpVtbl = &PropertyBag_Vtbl;
297     This->ref = 1;
298     This->prop_count = count;
299
300     if (count == 0)
301     {
302         This->properties = NULL;
303         This->values = NULL;
304     }
305     else
306     {
307         This->properties = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PROPBAG2)*count);
308         This->values = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(VARIANT)*count);
309
310         if (!This->properties || !This->values)
311             res = E_OUTOFMEMORY;
312         else
313             for (i=0; i < count; i++)
314             {
315                 res = copy_propbag2(This->properties+i, options+i, FALSE);
316                 if (FAILED(res))
317                     break;
318                 This->properties[i].dwHint = i+1; /* 0 means unset, so we start with 1 */
319             }
320     }
321
322     if (FAILED(res))
323     {
324         PropertyBag_Release(&This->IPropertyBag2_iface);
325         *ppPropertyBag2 = NULL;
326     }
327     else
328         *ppPropertyBag2 = &This->IPropertyBag2_iface;
329
330     return res;
331 }