msxml3: Add a stub IXSLProcessor.
[wine/multimedia.git] / dlls / quartz / filtermapper.c
blob30434dba3eb67973b47b60cc7554609a46b62e2d
1 /*
2 * IFilterMapper & IFilterMapper3 Implementations
4 * Copyright 2003 Robert Shearman
5 * Copyright 2004 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winerror.h"
32 #include "quartz_private.h"
34 #include "ole2.h"
35 #include "olectl.h"
36 #include "strmif.h"
37 #include "wine/unicode.h"
38 #include "uuids.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
44 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
46 /* Unexposed IAMFilterData interface */
47 typedef struct IAMFilterData IAMFilterData;
49 typedef struct IAMFilterDataVtbl
51 BEGIN_INTERFACE
53 /*** IUnknown methods ***/
54 HRESULT (STDMETHODCALLTYPE *QueryInterface)(
55 IAMFilterData *This,
56 REFIID riid,
57 void **ppvObject);
59 ULONG (STDMETHODCALLTYPE *AddRef)(
60 IAMFilterData *This);
62 ULONG (STDMETHODCALLTYPE *Release)(
63 IAMFilterData *This);
65 /*** IAMFilterData methods ***/
66 HRESULT (STDMETHODCALLTYPE *ParseFilterData)(
67 IAMFilterData *This,
68 BYTE *pData,
69 ULONG cb,
70 BYTE **ppRegFilter2);
72 HRESULT (STDMETHODCALLTYPE *CreateFilterData)(
73 IAMFilterData* This,
74 REGFILTER2 *prf2,
75 BYTE **pRegFilterData,
76 ULONG *pcb);
78 END_INTERFACE
79 } IAMFilterDataVtbl;
80 struct IAMFilterData
82 const IAMFilterDataVtbl *lpVtbl;
84 static const GUID IID_IAMFilterData = {
85 0x97f7c4d4, 0x547b, 0x4a5f, { 0x83,0x32, 0x53,0x64,0x30,0xad,0x2e,0x4d }
89 typedef struct FilterMapper3Impl
91 const IFilterMapper3Vtbl *lpVtbl;
92 const IFilterMapperVtbl *lpVtblFilterMapper;
93 const IAMFilterDataVtbl *lpVtblAMFilterData;
94 const IUnknownVtbl *IInner_vtbl;
95 LONG refCount;
96 IUnknown * pUnkOuter;
97 BOOL bUnkOuterValid;
98 BOOL bAggregatable;
99 } FilterMapper3Impl;
101 static const IUnknownVtbl IInner_VTable;
102 static const IFilterMapper3Vtbl fm3vtbl;
103 static const IFilterMapperVtbl fmvtbl;
104 static const IAMFilterDataVtbl AMFilterDataVtbl;
106 static inline FilterMapper3Impl *impl_from_IFilterMapper( IFilterMapper *iface )
108 return (FilterMapper3Impl *)((char*)iface - FIELD_OFFSET(FilterMapper3Impl, lpVtblFilterMapper));
111 static inline FilterMapper3Impl *impl_from_IAMFilterData( IAMFilterData *iface )
113 return (FilterMapper3Impl *)((char*)iface - FIELD_OFFSET(FilterMapper3Impl, lpVtblAMFilterData));
116 static inline FilterMapper3Impl *impl_from_inner_IUnknown( IUnknown *iface )
118 return (FilterMapper3Impl *)((char*)iface - FIELD_OFFSET(FilterMapper3Impl, IInner_vtbl));
121 static const WCHAR wszClsidSlash[] = {'C','L','S','I','D','\\',0};
122 static const WCHAR wszSlashInstance[] = {'\\','I','n','s','t','a','n','c','e','\\',0};
123 static const WCHAR wszSlash[] = {'\\',0};
125 /* CLSID property in media category Moniker */
126 static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
127 /* FriendlyName property in media category Moniker */
128 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
129 /* Merit property in media category Moniker (CLSID_ActiveMovieCategories only) */
130 static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
131 /* FilterData property in media category Moniker (not CLSID_ActiveMovieCategories) */
132 static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
133 /* For filters registered with IFilterMapper */
134 static const WCHAR wszFilterSlash[] = {'F','i','l','t','e','r','\\',0};
135 static const WCHAR wszFilter[] = {'F','i','l','t','e','r',0};
136 /* For pins registered with IFilterMapper */
137 static const WCHAR wszPins[] = {'P','i','n','s',0};
138 static const WCHAR wszAllowedMany[] = {'A','l','l','o','w','e','d','M','a','n','y',0};
139 static const WCHAR wszAllowedZero[] = {'A','l','l','o','w','e','d','Z','e','r','o',0};
140 static const WCHAR wszDirection[] = {'D','i','r','e','c','t','i','o','n',0};
141 static const WCHAR wszIsRendered[] = {'I','s','R','e','n','d','e','r','e','d',0};
142 /* For types registered with IFilterMapper */
143 static const WCHAR wszTypes[] = {'T','y','p','e','s',0};
146 /* registry format for REGFILTER2 */
147 struct REG_RF
149 DWORD dwVersion;
150 DWORD dwMerit;
151 DWORD dwPins;
152 DWORD dwUnused;
155 struct REG_RFP
157 BYTE signature[4]; /* e.g. "0pi3" */
158 DWORD dwFlags;
159 DWORD dwInstances;
160 DWORD dwMediaTypes;
161 DWORD dwMediums;
162 DWORD bCategory; /* is there a category clsid? */
163 /* optional: dwOffsetCategoryClsid */
166 struct REG_TYPE
168 BYTE signature[4]; /* e.g. "0ty3" */
169 DWORD dwUnused;
170 DWORD dwOffsetMajor;
171 DWORD dwOffsetMinor;
174 struct MONIKER_MERIT
176 IMoniker * pMoniker;
177 DWORD dwMerit;
180 struct Vector
182 LPBYTE pData;
183 int capacity; /* in bytes */
184 int current; /* pointer to next free byte */
187 /* returns the position it was added at */
188 static int add_data(struct Vector * v, const BYTE * pData, int size)
190 int index = v->current;
191 if (v->current + size > v->capacity)
193 LPBYTE pOldData = v->pData;
194 v->capacity = (v->capacity + size) * 2;
195 v->pData = CoTaskMemAlloc(v->capacity);
196 memcpy(v->pData, pOldData, v->current);
197 CoTaskMemFree(pOldData);
199 memcpy(v->pData + v->current, pData, size);
200 v->current += size;
201 return index;
204 static int find_data(const struct Vector * v, const BYTE * pData, int size)
206 int index;
207 for (index = 0; index < v->current; index++)
208 if (!memcmp(v->pData + index, pData, size))
209 return index;
210 /* not found */
211 return -1;
214 static void delete_vector(struct Vector * v)
216 CoTaskMemFree(v->pData);
217 v->current = 0;
218 v->capacity = 0;
221 HRESULT FilterMapper2_create(IUnknown *pUnkOuter, LPVOID *ppObj)
223 FilterMapper3Impl * pFM2impl;
225 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
227 pFM2impl = CoTaskMemAlloc(sizeof(*pFM2impl));
228 if (!pFM2impl)
229 return E_OUTOFMEMORY;
231 pFM2impl->pUnkOuter = pUnkOuter;
232 pFM2impl->bUnkOuterValid = FALSE;
233 pFM2impl->bAggregatable = FALSE;
234 pFM2impl->IInner_vtbl = &IInner_VTable;
235 pFM2impl->lpVtbl = &fm3vtbl;
236 pFM2impl->lpVtblFilterMapper = &fmvtbl;
237 pFM2impl->lpVtblAMFilterData = &AMFilterDataVtbl;
238 pFM2impl->refCount = 1;
240 *ppObj = pFM2impl;
242 TRACE("-- created at %p\n", pFM2impl);
244 return S_OK;
247 HRESULT FilterMapper_create(IUnknown *pUnkOuter, LPVOID *ppObj)
249 FilterMapper3Impl *pFM2impl;
250 HRESULT hr;
252 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
254 hr = FilterMapper2_create(pUnkOuter, (LPVOID*)&pFM2impl);
255 if (FAILED(hr))
256 return hr;
258 *ppObj = &pFM2impl->lpVtblFilterMapper;
260 return hr;
263 /*** IUnknown (inner) methods ***/
265 static HRESULT WINAPI Inner_QueryInterface(IUnknown * iface, REFIID riid, LPVOID * ppv)
267 FilterMapper3Impl *This = impl_from_inner_IUnknown(iface);
268 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
270 if (This->bAggregatable)
271 This->bUnkOuterValid = TRUE;
273 *ppv = NULL;
275 if (IsEqualIID(riid, &IID_IUnknown))
276 *ppv = &This->IInner_vtbl;
277 else if (IsEqualIID(riid, &IID_IFilterMapper2) ||
278 IsEqualIID(riid, &IID_IFilterMapper3))
279 *ppv = This;
280 else if (IsEqualIID(riid, &IID_IFilterMapper))
281 *ppv = &This->lpVtblFilterMapper;
282 else if (IsEqualIID(riid, &IID_IAMFilterData))
283 *ppv = &This->lpVtblAMFilterData;
285 if (*ppv != NULL)
287 IUnknown_AddRef((IUnknown *)*ppv);
288 return S_OK;
291 FIXME("No interface for %s\n", debugstr_guid(riid));
292 return E_NOINTERFACE;
295 static ULONG WINAPI Inner_AddRef(IUnknown * iface)
297 FilterMapper3Impl *This = impl_from_inner_IUnknown(iface);
298 ULONG refCount = InterlockedIncrement(&This->refCount);
300 TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
302 return refCount;
305 static ULONG WINAPI Inner_Release(IUnknown * iface)
307 FilterMapper3Impl *This = impl_from_inner_IUnknown(iface);
308 ULONG refCount = InterlockedDecrement(&This->refCount);
310 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
312 if (refCount == 0)
314 CoTaskMemFree(This);
315 return 0;
317 return refCount;
320 static const IUnknownVtbl IInner_VTable =
322 Inner_QueryInterface,
323 Inner_AddRef,
324 Inner_Release
327 static HRESULT WINAPI FilterMapper3_QueryInterface(IFilterMapper3 * iface, REFIID riid, LPVOID *ppv)
329 FilterMapper3Impl *This = (FilterMapper3Impl *)iface;
331 if (This->bAggregatable)
332 This->bUnkOuterValid = TRUE;
334 if (This->pUnkOuter)
336 if (This->bAggregatable)
337 return IUnknown_QueryInterface(This->pUnkOuter, riid, ppv);
339 if (IsEqualIID(riid, &IID_IUnknown))
341 HRESULT hr;
343 IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
344 hr = IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
345 IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
346 This->bAggregatable = TRUE;
347 return hr;
350 *ppv = NULL;
351 return E_NOINTERFACE;
354 return IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
357 static ULONG WINAPI FilterMapper3_AddRef(IFilterMapper3 * iface)
359 FilterMapper3Impl *This = (FilterMapper3Impl *)iface;
361 if (This->pUnkOuter && This->bUnkOuterValid)
362 return IUnknown_AddRef(This->pUnkOuter);
363 return IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
366 static ULONG WINAPI FilterMapper3_Release(IFilterMapper3 * iface)
368 FilterMapper3Impl *This = (FilterMapper3Impl *)iface;
370 if (This->pUnkOuter && This->bUnkOuterValid)
371 return IUnknown_Release(This->pUnkOuter);
372 return IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
375 /*** IFilterMapper3 methods ***/
377 static HRESULT WINAPI FilterMapper3_CreateCategory(
378 IFilterMapper3 * iface,
379 REFCLSID clsidCategory,
380 DWORD dwCategoryMerit,
381 LPCWSTR szDescription)
383 LPWSTR wClsidAMCat = NULL;
384 LPWSTR wClsidCategory = NULL;
385 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + ARRAYSIZE(wszSlashInstance)-1 + (CHARS_IN_GUID-1) * 2 + 1];
386 HKEY hKey = NULL;
387 LONG lRet;
388 HRESULT hr;
390 TRACE("(%s, %x, %s)\n", debugstr_guid(clsidCategory), dwCategoryMerit, debugstr_w(szDescription));
392 hr = StringFromCLSID(&CLSID_ActiveMovieCategories, &wClsidAMCat);
394 if (SUCCEEDED(hr))
396 hr = StringFromCLSID(clsidCategory, &wClsidCategory);
399 if (SUCCEEDED(hr))
401 strcpyW(wszKeyName, wszClsidSlash);
402 strcatW(wszKeyName, wClsidAMCat);
403 strcatW(wszKeyName, wszSlashInstance);
404 strcatW(wszKeyName, wClsidCategory);
406 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
407 hr = HRESULT_FROM_WIN32(lRet);
410 if (SUCCEEDED(hr))
412 lRet = RegSetValueExW(hKey, wszFriendlyName, 0, REG_SZ, (const BYTE*)szDescription, (strlenW(szDescription) + 1) * sizeof(WCHAR));
413 hr = HRESULT_FROM_WIN32(lRet);
416 if (SUCCEEDED(hr))
418 lRet = RegSetValueExW(hKey, wszClsidName, 0, REG_SZ, (LPBYTE)wClsidCategory, (strlenW(wClsidCategory) + 1) * sizeof(WCHAR));
419 hr = HRESULT_FROM_WIN32(lRet);
422 if (SUCCEEDED(hr))
424 lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwCategoryMerit, sizeof(dwCategoryMerit));
425 hr = HRESULT_FROM_WIN32(lRet);
428 CloseHandle(hKey);
429 CoTaskMemFree(wClsidCategory);
430 CoTaskMemFree(wClsidAMCat);
432 return hr;
435 static HRESULT WINAPI FilterMapper3_UnregisterFilter(
436 IFilterMapper3 * iface,
437 const CLSID *pclsidCategory,
438 const OLECHAR *szInstance,
439 REFCLSID Filter)
441 WCHAR wszKeyName[MAX_PATH];
442 LPWSTR wClsidCategory = NULL;
443 LPWSTR wFilter = NULL;
444 HRESULT hr;
446 TRACE("(%p, %s, %s)\n", pclsidCategory, debugstr_w(szInstance), debugstr_guid(Filter));
448 if (!pclsidCategory)
449 pclsidCategory = &CLSID_LegacyAmFilterCategory;
451 hr = StringFromCLSID(pclsidCategory, &wClsidCategory);
453 if (SUCCEEDED(hr))
455 strcpyW(wszKeyName, wszClsidSlash);
456 strcatW(wszKeyName, wClsidCategory);
457 strcatW(wszKeyName, wszSlashInstance);
458 if (szInstance)
459 strcatW(wszKeyName, szInstance);
460 else
462 hr = StringFromCLSID(Filter, &wFilter);
463 if (SUCCEEDED(hr))
464 strcatW(wszKeyName, wFilter);
468 if (SUCCEEDED(hr))
470 LONG lRet = RegDeleteKeyW(HKEY_CLASSES_ROOT, wszKeyName);
471 hr = HRESULT_FROM_WIN32(lRet);
474 CoTaskMemFree(wClsidCategory);
475 CoTaskMemFree(wFilter);
477 return hr;
480 static HRESULT FM2_WriteFriendlyName(IPropertyBag * pPropBag, LPCWSTR szName)
482 VARIANT var;
483 HRESULT ret;
484 BSTR value;
486 V_VT(&var) = VT_BSTR;
487 V_UNION(&var, bstrVal) = value = SysAllocString(szName);
489 ret = IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
490 SysFreeString(value);
492 return ret;
495 static HRESULT FM2_WriteClsid(IPropertyBag * pPropBag, REFCLSID clsid)
497 LPWSTR wszClsid = NULL;
498 VARIANT var;
499 HRESULT hr;
501 hr = StringFromCLSID(clsid, &wszClsid);
503 if (SUCCEEDED(hr))
505 V_VT(&var) = VT_BSTR;
506 V_UNION(&var, bstrVal) = wszClsid;
507 hr = IPropertyBag_Write(pPropBag, wszClsidName, &var);
509 CoTaskMemFree(wszClsid);
510 return hr;
513 static HRESULT FM2_WriteFilterData(const REGFILTER2 * prf2, BYTE **ppData, ULONG *pcbData)
515 int size = sizeof(struct REG_RF);
516 unsigned int i;
517 struct Vector mainStore = {NULL, 0, 0};
518 struct Vector clsidStore = {NULL, 0, 0};
519 struct REG_RF rrf;
520 HRESULT hr = S_OK;
522 rrf.dwVersion = prf2->dwVersion;
523 rrf.dwMerit = prf2->dwMerit;
524 rrf.dwPins = prf2->u.s1.cPins2;
525 rrf.dwUnused = 0;
527 add_data(&mainStore, (LPBYTE)&rrf, sizeof(rrf));
529 for (i = 0; i < prf2->u.s1.cPins2; i++)
531 size += sizeof(struct REG_RFP);
532 if (prf2->u.s1.rgPins2[i].clsPinCategory)
533 size += sizeof(DWORD);
534 size += prf2->u.s1.rgPins2[i].nMediaTypes * sizeof(struct REG_TYPE);
535 size += prf2->u.s1.rgPins2[i].nMediums * sizeof(DWORD);
538 for (i = 0; i < prf2->u.s1.cPins2; i++)
540 struct REG_RFP rrfp;
541 REGFILTERPINS2 rgPin2 = prf2->u.s1.rgPins2[i];
542 unsigned int j;
544 rrfp.signature[0] = '0';
545 rrfp.signature[1] = 'p';
546 rrfp.signature[2] = 'i';
547 rrfp.signature[3] = '3';
548 rrfp.signature[0] += i;
549 rrfp.dwFlags = rgPin2.dwFlags;
550 rrfp.dwInstances = rgPin2.cInstances;
551 rrfp.dwMediaTypes = rgPin2.nMediaTypes;
552 rrfp.dwMediums = rgPin2.nMediums;
553 rrfp.bCategory = rgPin2.clsPinCategory ? 1 : 0;
555 add_data(&mainStore, (LPBYTE)&rrfp, sizeof(rrfp));
556 if (rrfp.bCategory)
558 DWORD index = find_data(&clsidStore, (const BYTE*)rgPin2.clsPinCategory, sizeof(CLSID));
559 if (index == -1)
560 index = add_data(&clsidStore, (const BYTE*)rgPin2.clsPinCategory, sizeof(CLSID));
561 index += size;
563 add_data(&mainStore, (LPBYTE)&index, sizeof(index));
566 for (j = 0; j < rgPin2.nMediaTypes; j++)
568 struct REG_TYPE rt;
569 const CLSID * clsMinorType = rgPin2.lpMediaType[j].clsMinorType ? rgPin2.lpMediaType[j].clsMinorType : &MEDIASUBTYPE_NULL;
570 rt.signature[0] = '0';
571 rt.signature[1] = 't';
572 rt.signature[2] = 'y';
573 rt.signature[3] = '3';
574 rt.signature[0] += j;
575 rt.dwUnused = 0;
576 rt.dwOffsetMajor = find_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
577 if (rt.dwOffsetMajor == -1)
578 rt.dwOffsetMajor = add_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
579 rt.dwOffsetMajor += size;
580 rt.dwOffsetMinor = find_data(&clsidStore, (const BYTE*)clsMinorType, sizeof(CLSID));
581 if (rt.dwOffsetMinor == -1)
582 rt.dwOffsetMinor = add_data(&clsidStore, (const BYTE*)clsMinorType, sizeof(CLSID));
583 rt.dwOffsetMinor += size;
585 add_data(&mainStore, (LPBYTE)&rt, sizeof(rt));
588 for (j = 0; j < rgPin2.nMediums; j++)
590 DWORD index = find_data(&clsidStore, (const BYTE*)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
591 if (index == -1)
592 index = add_data(&clsidStore, (const BYTE*)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
593 index += size;
595 add_data(&mainStore, (LPBYTE)&index, sizeof(index));
599 if (SUCCEEDED(hr))
601 *pcbData = mainStore.current + clsidStore.current;
602 *ppData = CoTaskMemAlloc(*pcbData);
603 if (!*ppData)
604 hr = E_OUTOFMEMORY;
607 if (SUCCEEDED(hr))
609 memcpy(*ppData, mainStore.pData, mainStore.current);
610 memcpy((*ppData) + mainStore.current, clsidStore.pData, clsidStore.current);
613 delete_vector(&mainStore);
614 delete_vector(&clsidStore);
615 return hr;
618 static HRESULT FM2_ReadFilterData(BYTE *pData, REGFILTER2 * prf2)
620 HRESULT hr = S_OK;
621 struct REG_RF * prrf;
622 LPBYTE pCurrent;
623 DWORD i;
624 REGFILTERPINS2 * rgPins2;
626 prrf = (struct REG_RF *)pData;
627 pCurrent = pData;
629 if (prrf->dwVersion != 2)
631 FIXME("Filter registry version %d not supported\n", prrf->dwVersion);
632 ZeroMemory(prf2, sizeof(*prf2));
633 hr = E_FAIL;
636 if (SUCCEEDED(hr))
638 TRACE("version = %d, merit = %x, #pins = %d, unused = %x\n",
639 prrf->dwVersion, prrf->dwMerit, prrf->dwPins, prrf->dwUnused);
641 prf2->dwVersion = prrf->dwVersion;
642 prf2->dwMerit = prrf->dwMerit;
643 prf2->u.s1.cPins2 = prrf->dwPins;
644 rgPins2 = CoTaskMemAlloc(prrf->dwPins * sizeof(*rgPins2));
645 prf2->u.s1.rgPins2 = rgPins2;
646 pCurrent += sizeof(struct REG_RF);
648 for (i = 0; i < prrf->dwPins; i++)
650 struct REG_RFP * prrfp = (struct REG_RFP *)pCurrent;
651 REGPINTYPES * lpMediaType;
652 REGPINMEDIUM * lpMedium;
653 UINT j;
655 /* FIXME: check signature */
657 TRACE("\tsignature = %s\n", debugstr_an((const char*)prrfp->signature, 4));
659 TRACE("\tpin[%d]: flags = %x, instances = %d, media types = %d, mediums = %d\n",
660 i, prrfp->dwFlags, prrfp->dwInstances, prrfp->dwMediaTypes, prrfp->dwMediums);
662 rgPins2[i].dwFlags = prrfp->dwFlags;
663 rgPins2[i].cInstances = prrfp->dwInstances;
664 rgPins2[i].nMediaTypes = prrfp->dwMediaTypes;
665 rgPins2[i].nMediums = prrfp->dwMediums;
666 pCurrent += sizeof(struct REG_RFP);
667 if (prrfp->bCategory)
669 CLSID * clsCat = CoTaskMemAlloc(sizeof(CLSID));
670 memcpy(clsCat, pData + *(DWORD*)(pCurrent), sizeof(CLSID));
671 pCurrent += sizeof(DWORD);
672 rgPins2[i].clsPinCategory = clsCat;
674 else
675 rgPins2[i].clsPinCategory = NULL;
677 if (rgPins2[i].nMediaTypes > 0)
678 lpMediaType = CoTaskMemAlloc(rgPins2[i].nMediaTypes * sizeof(*lpMediaType));
679 else
680 lpMediaType = NULL;
682 rgPins2[i].lpMediaType = lpMediaType;
684 for (j = 0; j < rgPins2[i].nMediaTypes; j++)
686 struct REG_TYPE * prt = (struct REG_TYPE *)pCurrent;
687 CLSID * clsMajor = CoTaskMemAlloc(sizeof(CLSID));
688 CLSID * clsMinor = CoTaskMemAlloc(sizeof(CLSID));
690 /* FIXME: check signature */
691 TRACE("\t\tsignature = %s\n", debugstr_an((const char*)prt->signature, 4));
693 memcpy(clsMajor, pData + prt->dwOffsetMajor, sizeof(CLSID));
694 memcpy(clsMinor, pData + prt->dwOffsetMinor, sizeof(CLSID));
696 lpMediaType[j].clsMajorType = clsMajor;
697 lpMediaType[j].clsMinorType = clsMinor;
699 pCurrent += sizeof(*prt);
702 if (rgPins2[i].nMediums > 0)
703 lpMedium = CoTaskMemAlloc(rgPins2[i].nMediums * sizeof(*lpMedium));
704 else
705 lpMedium = NULL;
707 rgPins2[i].lpMedium = lpMedium;
709 for (j = 0; j < rgPins2[i].nMediums; j++)
711 DWORD dwOffset = *(DWORD *)pCurrent;
713 memcpy(lpMedium + j, pData + dwOffset, sizeof(REGPINMEDIUM));
715 pCurrent += sizeof(dwOffset);
721 return hr;
724 static void FM2_DeleteRegFilter(REGFILTER2 * prf2)
726 UINT i;
727 for (i = 0; i < prf2->u.s1.cPins2; i++)
729 UINT j;
730 if (prf2->u.s1.rgPins2[i].clsPinCategory)
731 CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].clsPinCategory);
733 for (j = 0; j < prf2->u.s1.rgPins2[i].nMediaTypes; j++)
735 CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMediaType[j].clsMajorType);
736 CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMediaType[j].clsMinorType);
738 CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMediaType);
739 CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMedium);
741 CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2);
744 static HRESULT WINAPI FilterMapper3_RegisterFilter(
745 IFilterMapper3 * iface,
746 REFCLSID clsidFilter,
747 LPCWSTR szName,
748 IMoniker **ppMoniker,
749 const CLSID *pclsidCategory,
750 const OLECHAR *szInstance,
751 const REGFILTER2 *prf2)
753 IParseDisplayName * pParser = NULL;
754 IBindCtx * pBindCtx = NULL;
755 IMoniker * pMoniker = NULL;
756 IPropertyBag * pPropBag = NULL;
757 HRESULT hr;
758 LPWSTR pwszParseName = NULL;
759 LPWSTR pCurrent;
760 static const WCHAR wszDevice[] = {'@','d','e','v','i','c','e',':','s','w',':',0};
761 int nameLen;
762 ULONG ulEaten;
763 LPWSTR szClsidTemp = NULL;
764 REGFILTER2 regfilter2;
765 REGFILTERPINS2* pregfp2 = NULL;
767 TRACE("(%s, %s, %p, %s, %s, %p)\n",
768 debugstr_guid(clsidFilter),
769 debugstr_w(szName),
770 ppMoniker,
771 debugstr_guid(pclsidCategory),
772 debugstr_w(szInstance),
773 prf2);
775 if (prf2->dwVersion == 2)
777 regfilter2 = *prf2;
779 else if (prf2->dwVersion == 1)
781 ULONG i;
782 DWORD flags;
783 /* REGFILTER2 structure is converted from version 1 to 2. Tested on Win2k. */
784 regfilter2.dwVersion = 2;
785 regfilter2.dwMerit = prf2->dwMerit;
786 regfilter2.u.s1.cPins2 = prf2->u.s.cPins;
787 pregfp2 = CoTaskMemAlloc(prf2->u.s.cPins * sizeof(REGFILTERPINS2));
788 regfilter2.u.s1.rgPins2 = pregfp2;
789 for (i = 0; i < prf2->u.s.cPins; i++)
791 flags = 0;
792 if (prf2->u.s.rgPins[i].bRendered)
793 flags |= REG_PINFLAG_B_RENDERER;
794 if (prf2->u.s.rgPins[i].bOutput)
795 flags |= REG_PINFLAG_B_OUTPUT;
796 if (prf2->u.s.rgPins[i].bZero)
797 flags |= REG_PINFLAG_B_ZERO;
798 if (prf2->u.s.rgPins[i].bMany)
799 flags |= REG_PINFLAG_B_MANY;
800 pregfp2[i].dwFlags = flags;
801 pregfp2[i].cInstances = 1;
802 pregfp2[i].nMediaTypes = prf2->u.s.rgPins[i].nMediaTypes;
803 pregfp2[i].lpMediaType = prf2->u.s.rgPins[i].lpMediaType;
804 pregfp2[i].nMediums = 0;
805 pregfp2[i].lpMedium = NULL;
806 pregfp2[i].clsPinCategory = NULL;
809 else
811 FIXME("dwVersion other that 1 or 2 not supported at the moment\n");
812 return E_NOTIMPL;
815 if (ppMoniker)
816 *ppMoniker = NULL;
818 if (!pclsidCategory)
819 /* MSDN mentions the inexistent CLSID_ActiveMovieFilters GUID.
820 * In fact this is the CLSID_LegacyAmFilterCategory one */
821 pclsidCategory = &CLSID_LegacyAmFilterCategory;
823 /* sizeof... will include the null terminator and
824 * the + 1 is for the separator ('\\'). The -1 is
825 * because CHARS_IN_GUID includes the null terminator
827 nameLen = sizeof(wszDevice)/sizeof(wszDevice[0]) + CHARS_IN_GUID - 1 + 1;
829 if (szInstance)
830 nameLen += strlenW(szInstance);
831 else
832 nameLen += CHARS_IN_GUID - 1; /* CHARS_IN_GUID includes null terminator */
834 pCurrent = pwszParseName = CoTaskMemAlloc(nameLen*sizeof(WCHAR));
835 if (!pwszParseName)
836 return E_OUTOFMEMORY;
838 strcpyW(pwszParseName, wszDevice);
839 pCurrent += strlenW(wszDevice);
841 hr = StringFromCLSID(pclsidCategory, &szClsidTemp);
843 if (SUCCEEDED(hr))
845 memcpy(pCurrent, szClsidTemp, CHARS_IN_GUID * sizeof(WCHAR));
846 pCurrent += CHARS_IN_GUID - 1;
847 pCurrent[0] = '\\';
849 if (szInstance)
850 strcpyW(pCurrent+1, szInstance);
851 else
853 CoTaskMemFree(szClsidTemp);
854 szClsidTemp = NULL;
856 hr = StringFromCLSID(clsidFilter, &szClsidTemp);
857 if (SUCCEEDED(hr))
858 strcpyW(pCurrent+1, szClsidTemp);
862 if (SUCCEEDED(hr))
863 hr = CoCreateInstance(&CLSID_CDeviceMoniker, NULL, CLSCTX_INPROC, &IID_IParseDisplayName, (LPVOID *)&pParser);
865 if (SUCCEEDED(hr))
866 hr = CreateBindCtx(0, &pBindCtx);
868 if (SUCCEEDED(hr))
869 hr = IParseDisplayName_ParseDisplayName(pParser, pBindCtx, pwszParseName, &ulEaten, &pMoniker);
871 if (pBindCtx)
872 IBindCtx_Release(pBindCtx);
873 if (pParser)
874 IParseDisplayName_Release(pParser);
876 if (SUCCEEDED(hr))
877 hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
879 if (SUCCEEDED(hr))
880 hr = FM2_WriteFriendlyName(pPropBag, szName);
882 if (SUCCEEDED(hr))
883 hr = FM2_WriteClsid(pPropBag, clsidFilter);
885 if (SUCCEEDED(hr))
887 BYTE *pData;
888 ULONG cbData;
890 hr = FM2_WriteFilterData(&regfilter2, &pData, &cbData);
891 if (SUCCEEDED(hr))
893 VARIANT var;
894 SAFEARRAY *psa;
895 SAFEARRAYBOUND saBound;
897 saBound.lLbound = 0;
898 saBound.cElements = cbData;
899 psa = SafeArrayCreate(VT_UI1, 1, &saBound);
900 if (!psa)
902 ERR("Couldn't create SAFEARRAY\n");
903 hr = E_FAIL;
906 if (SUCCEEDED(hr))
908 LPBYTE pbSAData;
909 hr = SafeArrayAccessData(psa, (LPVOID *)&pbSAData);
910 if (SUCCEEDED(hr))
912 memcpy(pbSAData, pData, cbData);
913 hr = SafeArrayUnaccessData(psa);
917 V_VT(&var) = VT_ARRAY | VT_UI1;
918 V_UNION(&var, parray) = psa;
920 if (SUCCEEDED(hr))
921 hr = IPropertyBag_Write(pPropBag, wszFilterDataName, &var);
923 if (psa)
924 SafeArrayDestroy(psa);
925 CoTaskMemFree(pData);
929 if (pPropBag)
930 IPropertyBag_Release(pPropBag);
931 CoTaskMemFree(szClsidTemp);
932 CoTaskMemFree(pwszParseName);
934 if (SUCCEEDED(hr) && ppMoniker)
935 *ppMoniker = pMoniker;
936 else if (pMoniker)
937 IMoniker_Release(pMoniker);
939 CoTaskMemFree(pregfp2);
941 TRACE("-- returning %x\n", hr);
943 return hr;
946 /* internal helper function */
947 static BOOL MatchTypes(
948 BOOL bExactMatch,
949 DWORD nPinTypes,
950 const REGPINTYPES * pPinTypes,
951 DWORD nMatchTypes,
952 const GUID * pMatchTypes)
954 BOOL bMatch = FALSE;
955 DWORD j;
957 if ((nMatchTypes == 0) && (nPinTypes > 0))
958 bMatch = TRUE;
960 for (j = 0; j < nPinTypes; j++)
962 DWORD i;
963 for (i = 0; i < nMatchTypes*2; i+=2)
965 if (((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMajorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMajorType, &pMatchTypes[i])) &&
966 ((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMinorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i+1], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMinorType, &pMatchTypes[i+1])))
968 bMatch = TRUE;
969 break;
973 return bMatch;
976 /* internal helper function for qsort of MONIKER_MERIT array */
977 static int mm_compare(const void * left, const void * right)
979 const struct MONIKER_MERIT * mmLeft = left;
980 const struct MONIKER_MERIT * mmRight = right;
982 if (mmLeft->dwMerit == mmRight->dwMerit)
983 return 0;
984 if (mmLeft->dwMerit > mmRight->dwMerit)
985 return -1;
986 return 1;
989 /* NOTES:
990 * Exact match means whether or not to treat GUID_NULL's in filter data as wild cards
991 * (GUID_NULL's in input to function automatically treated as wild cards)
992 * Input/Output needed means match only on criteria if TRUE (with zero input types
993 * meaning match any input/output pin as long as one exists), otherwise match any
994 * filter that meets the rest of the requirements.
996 static HRESULT WINAPI FilterMapper3_EnumMatchingFilters(
997 IFilterMapper3 * iface,
998 IEnumMoniker **ppEnum,
999 DWORD dwFlags,
1000 BOOL bExactMatch,
1001 DWORD dwMerit,
1002 BOOL bInputNeeded,
1003 DWORD cInputTypes,
1004 const GUID *pInputTypes,
1005 const REGPINMEDIUM *pMedIn,
1006 const CLSID *pPinCategoryIn,
1007 BOOL bRender,
1008 BOOL bOutputNeeded,
1009 DWORD cOutputTypes,
1010 const GUID *pOutputTypes,
1011 const REGPINMEDIUM *pMedOut,
1012 const CLSID *pPinCategoryOut)
1014 ICreateDevEnum * pCreateDevEnum;
1015 IMoniker * pMonikerCat;
1016 IEnumMoniker * pEnumCat;
1017 HRESULT hr;
1018 struct Vector monikers = {NULL, 0, 0};
1020 TRACE("(%p, %x, %s, %x, %s, %d, %p, %p, %p, %s, %s, %p, %p, %p)\n",
1021 ppEnum,
1022 dwFlags,
1023 bExactMatch ? "true" : "false",
1024 dwMerit,
1025 bInputNeeded ? "true" : "false",
1026 cInputTypes,
1027 pInputTypes,
1028 pMedIn,
1029 pPinCategoryIn,
1030 bRender ? "true" : "false",
1031 bOutputNeeded ? "true" : "false",
1032 pOutputTypes,
1033 pMedOut,
1034 pPinCategoryOut);
1036 if (dwFlags != 0)
1038 FIXME("dwFlags = %x not implemented\n", dwFlags);
1041 *ppEnum = NULL;
1043 hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (LPVOID*)&pCreateDevEnum);
1044 if (FAILED(hr))
1045 return hr;
1047 hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEnumCat, 0);
1048 if (FAILED(hr)) {
1049 ICreateDevEnum_Release(pCreateDevEnum);
1050 return hr;
1053 while (IEnumMoniker_Next(pEnumCat, 1, &pMonikerCat, NULL) == S_OK)
1055 IPropertyBag * pPropBagCat = NULL;
1056 VARIANT var;
1057 HRESULT hrSub; /* this is so that one buggy filter
1058 doesn't make the whole lot fail */
1060 VariantInit(&var);
1062 hrSub = IMoniker_BindToStorage(pMonikerCat, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
1064 if (SUCCEEDED(hrSub))
1065 hrSub = IPropertyBag_Read(pPropBagCat, wszMeritName, &var, NULL);
1067 if (SUCCEEDED(hrSub) && (V_UNION(&var, ulVal) >= dwMerit))
1069 CLSID clsidCat;
1070 IEnumMoniker * pEnum;
1071 IMoniker * pMoniker;
1073 VariantClear(&var);
1075 if (TRACE_ON(quartz))
1077 VARIANT temp;
1078 V_VT(&temp) = VT_EMPTY;
1079 IPropertyBag_Read(pPropBagCat, wszFriendlyName, &temp, NULL);
1080 TRACE("Considering category %s\n", debugstr_w(V_UNION(&temp, bstrVal)));
1081 VariantClear(&temp);
1084 hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
1086 if (SUCCEEDED(hrSub))
1087 hrSub = CLSIDFromString(V_UNION(&var, bstrVal), &clsidCat);
1089 if (SUCCEEDED(hrSub))
1090 hrSub = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
1092 if (hrSub == S_OK)
1094 while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
1096 IPropertyBag * pPropBag = NULL;
1097 VARIANT var;
1098 BYTE *pData = NULL;
1099 REGFILTER2 rf2;
1100 DWORD i;
1101 BOOL bInputMatch = !bInputNeeded;
1102 BOOL bOutputMatch = !bOutputNeeded;
1104 ZeroMemory(&rf2, sizeof(rf2));
1105 VariantInit(&var);
1107 hrSub = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBag);
1109 if (TRACE_ON(quartz))
1111 VARIANT temp;
1112 V_VT(&temp) = VT_EMPTY;
1113 IPropertyBag_Read(pPropBag, wszFriendlyName, &temp, NULL);
1114 TRACE("Considering filter %s\n", debugstr_w(V_UNION(&temp, bstrVal)));
1115 VariantClear(&temp);
1118 if (SUCCEEDED(hrSub))
1120 hrSub = IPropertyBag_Read(pPropBag, wszFilterDataName, &var, NULL);
1123 if (SUCCEEDED(hrSub))
1124 hrSub = SafeArrayAccessData(V_UNION(&var, parray), (LPVOID*)&pData);
1126 if (SUCCEEDED(hrSub))
1127 hrSub = FM2_ReadFilterData(pData, &rf2);
1129 if (pData)
1130 SafeArrayUnaccessData(V_UNION(&var, parray));
1132 VariantClear(&var);
1134 /* Logic used for bInputMatch expression:
1135 * There exists some pin such that bInputNeeded implies (pin is an input and
1136 * (bRender implies pin has render flag) and major/minor types members of
1137 * pInputTypes )
1138 * bOutputMatch is similar, but without the "bRender implies ..." part
1139 * and substituting variables names containing input for output
1142 /* determine whether filter meets requirements */
1143 if (SUCCEEDED(hrSub) && (rf2.dwMerit >= dwMerit))
1145 for (i = 0; (i < rf2.u.s1.cPins2) && (!bInputMatch || !bOutputMatch); i++)
1147 const REGFILTERPINS2 * rfp2 = rf2.u.s1.rgPins2 + i;
1149 bInputMatch = bInputMatch || (!(rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
1150 (!bRender || (rfp2->dwFlags & REG_PINFLAG_B_RENDERER)) &&
1151 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cInputTypes, pInputTypes));
1152 bOutputMatch = bOutputMatch || ((rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
1153 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cOutputTypes, pOutputTypes));
1156 if (bInputMatch && bOutputMatch)
1158 struct MONIKER_MERIT mm = {pMoniker, rf2.dwMerit};
1159 IMoniker_AddRef(pMoniker);
1160 add_data(&monikers, (LPBYTE)&mm, sizeof(mm));
1164 FM2_DeleteRegFilter(&rf2);
1165 if (pPropBag)
1166 IPropertyBag_Release(pPropBag);
1167 IMoniker_Release(pMoniker);
1169 IEnumMoniker_Release(pEnum);
1173 VariantClear(&var);
1174 if (pPropBagCat)
1175 IPropertyBag_Release(pPropBagCat);
1176 IMoniker_Release(pMonikerCat);
1179 if (SUCCEEDED(hr))
1181 IMoniker ** ppMoniker;
1182 unsigned int i;
1183 ULONG nMonikerCount = monikers.current / sizeof(struct MONIKER_MERIT);
1185 /* sort the monikers in descending merit order */
1186 qsort(monikers.pData, nMonikerCount,
1187 sizeof(struct MONIKER_MERIT),
1188 mm_compare);
1190 /* construct an IEnumMoniker interface */
1191 ppMoniker = CoTaskMemAlloc(nMonikerCount * sizeof(IMoniker *));
1192 for (i = 0; i < nMonikerCount; i++)
1194 /* no need to AddRef here as already AddRef'd above */
1195 ppMoniker[i] = ((struct MONIKER_MERIT *)monikers.pData)[i].pMoniker;
1197 hr = EnumMonikerImpl_Create(ppMoniker, nMonikerCount, ppEnum);
1198 CoTaskMemFree(ppMoniker);
1201 delete_vector(&monikers);
1202 IEnumMoniker_Release(pEnumCat);
1203 ICreateDevEnum_Release(pCreateDevEnum);
1205 return hr;
1208 static HRESULT WINAPI FilterMapper3_GetICreateDevEnum(IFilterMapper3 *iface, ICreateDevEnum **ppEnum)
1210 TRACE("(%p, %p)\n", iface, ppEnum);
1211 if (!ppEnum)
1212 return E_POINTER;
1213 return CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (void**)ppEnum);
1216 static const IFilterMapper3Vtbl fm3vtbl =
1219 FilterMapper3_QueryInterface,
1220 FilterMapper3_AddRef,
1221 FilterMapper3_Release,
1223 FilterMapper3_CreateCategory,
1224 FilterMapper3_UnregisterFilter,
1225 FilterMapper3_RegisterFilter,
1226 FilterMapper3_EnumMatchingFilters,
1227 FilterMapper3_GetICreateDevEnum
1230 /*** IUnknown methods ***/
1232 static HRESULT WINAPI FilterMapper_QueryInterface(IFilterMapper * iface, REFIID riid, LPVOID *ppv)
1234 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1236 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1238 return FilterMapper3_QueryInterface((IFilterMapper3*)&This->lpVtbl, riid, ppv);
1241 static ULONG WINAPI FilterMapper_AddRef(IFilterMapper * iface)
1243 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1245 return FilterMapper3_AddRef((IFilterMapper3*)This);
1248 static ULONG WINAPI FilterMapper_Release(IFilterMapper * iface)
1250 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1252 return FilterMapper3_Release((IFilterMapper3*)This);
1255 /*** IFilterMapper methods ***/
1257 static HRESULT WINAPI FilterMapper_EnumMatchingFilters(
1258 IFilterMapper * iface,
1259 IEnumRegFilters **ppEnum,
1260 DWORD dwMerit,
1261 BOOL bInputNeeded,
1262 CLSID clsInMaj,
1263 CLSID clsInSub,
1264 BOOL bRender,
1265 BOOL bOutputNeeded,
1266 CLSID clsOutMaj,
1267 CLSID clsOutSub)
1269 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1270 GUID InputType[2];
1271 GUID OutputType[2];
1272 IEnumMoniker* ppEnumMoniker;
1273 IMoniker* IMon;
1274 ULONG nb;
1275 ULONG idx = 0, nb_mon = 0;
1276 REGFILTER* regfilters;
1277 HRESULT hr;
1279 TRACE("(%p/%p)->(%p, %x, %s, %s, %s, %s, %s, %s, %s) stub!\n",
1280 iface,This,
1281 ppEnum,
1282 dwMerit,
1283 bInputNeeded ? "true" : "false",
1284 debugstr_guid(&clsInMaj),
1285 debugstr_guid(&clsInSub),
1286 bRender ? "true" : "false",
1287 bOutputNeeded ? "true" : "false",
1288 debugstr_guid(&clsOutMaj),
1289 debugstr_guid(&clsOutSub));
1291 InputType[0] = clsInMaj;
1292 InputType[1] = clsInSub;
1293 OutputType[0] = clsOutMaj;
1294 OutputType[1] = clsOutSub;
1296 *ppEnum = NULL;
1298 hr = IFilterMapper3_EnumMatchingFilters((IFilterMapper3*)This,
1299 &ppEnumMoniker,
1301 TRUE,
1302 dwMerit,
1303 bInputNeeded,
1305 InputType,
1306 NULL,
1307 &GUID_NULL,
1308 bRender,
1309 bOutputNeeded,
1311 OutputType,
1312 NULL,
1313 &GUID_NULL);
1315 if (FAILED(hr))
1316 return hr;
1318 while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1320 IMoniker_Release(IMon);
1321 nb_mon++;
1324 if (!nb_mon)
1326 IEnumMoniker_Release(ppEnumMoniker);
1327 return IEnumRegFiltersImpl_Construct(NULL, 0, ppEnum);
1330 regfilters = CoTaskMemAlloc(nb_mon * sizeof(REGFILTER));
1331 if (!regfilters)
1333 IEnumMoniker_Release(ppEnumMoniker);
1334 return E_OUTOFMEMORY;
1336 ZeroMemory(regfilters, nb_mon * sizeof(REGFILTER)); /* will prevent bad free of Name in case of error. */
1338 IEnumMoniker_Reset(ppEnumMoniker);
1339 while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1341 IPropertyBag * pPropBagCat = NULL;
1342 VARIANT var;
1343 HRESULT hrSub;
1344 GUID clsid;
1345 int len;
1347 VariantInit(&var);
1349 hrSub = IMoniker_BindToStorage(IMon, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
1351 if (SUCCEEDED(hrSub))
1352 hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
1354 if (SUCCEEDED(hrSub))
1355 hrSub = CLSIDFromString(V_UNION(&var, bstrVal), &clsid);
1357 VariantClear(&var);
1359 if (SUCCEEDED(hrSub))
1360 hrSub = IPropertyBag_Read(pPropBagCat, wszFriendlyName, &var, NULL);
1362 if (SUCCEEDED(hrSub))
1364 len = (strlenW(V_UNION(&var, bstrVal))+1) * sizeof(WCHAR);
1365 if (!(regfilters[idx].Name = CoTaskMemAlloc(len*2)))
1366 hr = E_OUTOFMEMORY;
1369 if (SUCCEEDED(hrSub) && regfilters[idx].Name)
1371 memcpy(regfilters[idx].Name, V_UNION(&var, bstrVal), len);
1372 regfilters[idx].Clsid = clsid;
1373 idx++;
1376 if (pPropBagCat)
1377 IPropertyBag_Release(pPropBagCat);
1378 IMoniker_Release(IMon);
1379 VariantClear(&var);
1382 if (SUCCEEDED(hr))
1384 hr = IEnumRegFiltersImpl_Construct(regfilters, nb_mon, ppEnum);
1387 for (idx = 0; idx < nb_mon; idx++)
1388 CoTaskMemFree(regfilters[idx].Name);
1389 CoTaskMemFree(regfilters);
1390 IEnumMoniker_Release(ppEnumMoniker);
1392 return hr;
1396 static HRESULT WINAPI FilterMapper_RegisterFilter(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, DWORD dwMerit)
1398 HRESULT hr;
1399 LPWSTR wszClsid = NULL;
1400 HKEY hKey;
1401 LONG lRet;
1402 WCHAR wszKeyName[ARRAYSIZE(wszFilterSlash)-1 + (CHARS_IN_GUID-1) + 1];
1404 TRACE("(%p)->(%s, %s, %x)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), dwMerit);
1406 hr = StringFromCLSID(&clsid, &wszClsid);
1408 if (SUCCEEDED(hr))
1410 strcpyW(wszKeyName, wszFilterSlash);
1411 strcatW(wszKeyName, wszClsid);
1413 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1414 hr = HRESULT_FROM_WIN32(lRet);
1417 if (SUCCEEDED(hr))
1419 lRet = RegSetValueExW(hKey, NULL, 0, REG_SZ, (const BYTE*)szName, (strlenW(szName) + 1) * sizeof(WCHAR));
1420 hr = HRESULT_FROM_WIN32(lRet);
1421 CloseHandle(hKey);
1424 if (SUCCEEDED(hr))
1426 strcpyW(wszKeyName, wszClsidSlash);
1427 strcatW(wszKeyName, wszClsid);
1429 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1430 hr = HRESULT_FROM_WIN32(lRet);
1433 if (SUCCEEDED(hr))
1435 lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwMerit, sizeof(dwMerit));
1436 hr = HRESULT_FROM_WIN32(lRet);
1437 CloseHandle(hKey);
1440 CoTaskMemFree(wszClsid);
1442 return hr;
1445 static HRESULT WINAPI FilterMapper_RegisterFilterInstance(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, CLSID *MRId)
1447 TRACE("(%p)->(%s, %s, %p)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), MRId);
1449 /* Not implemented in Windows (tested on Win2k) */
1451 return E_NOTIMPL;
1454 static HRESULT WINAPI FilterMapper_RegisterPin(
1455 IFilterMapper * iface,
1456 CLSID Filter,
1457 LPCWSTR szName,
1458 BOOL bRendered,
1459 BOOL bOutput,
1460 BOOL bZero,
1461 BOOL bMany,
1462 CLSID ConnectsToFilter,
1463 LPCWSTR ConnectsToPin)
1465 HRESULT hr;
1466 LONG lRet;
1467 LPWSTR wszClsid = NULL;
1468 HKEY hKey = NULL;
1469 HKEY hPinsKey = NULL;
1470 WCHAR * wszPinsKeyName;
1471 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1473 TRACE("(%p)->(%s, %s, %d, %d, %d, %d, %s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(szName), bRendered,
1474 bOutput, bZero, bMany, debugstr_guid(&ConnectsToFilter), debugstr_w(ConnectsToPin));
1476 hr = StringFromCLSID(&Filter, &wszClsid);
1478 if (SUCCEEDED(hr))
1480 strcpyW(wszKeyName, wszClsidSlash);
1481 strcatW(wszKeyName, wszClsid);
1483 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1484 hr = HRESULT_FROM_WIN32(lRet);
1487 if (SUCCEEDED(hr))
1489 wszPinsKeyName = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(szName) + 1) * 2);
1490 if (!wszPinsKeyName)
1491 hr = E_OUTOFMEMORY;
1494 if (SUCCEEDED(hr))
1496 strcpyW(wszPinsKeyName, wszPins);
1497 strcatW(wszPinsKeyName, wszSlash);
1498 strcatW(wszPinsKeyName, szName);
1500 lRet = RegCreateKeyExW(hKey, wszPinsKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hPinsKey, NULL);
1501 hr = HRESULT_FROM_WIN32(lRet);
1502 CoTaskMemFree(wszPinsKeyName);
1505 if (SUCCEEDED(hr))
1507 lRet = RegSetValueExW(hPinsKey, wszAllowedMany, 0, REG_DWORD, (LPBYTE)&bMany, sizeof(bMany));
1508 hr = HRESULT_FROM_WIN32(lRet);
1511 if (SUCCEEDED(hr))
1513 lRet = RegSetValueExW(hPinsKey, wszAllowedZero, 0, REG_DWORD, (LPBYTE)&bZero, sizeof(bZero));
1514 hr = HRESULT_FROM_WIN32(lRet);
1517 if (SUCCEEDED(hr))
1519 lRet = RegSetValueExW(hPinsKey, wszDirection, 0, REG_DWORD, (LPBYTE)&bOutput, sizeof(bOutput));
1520 hr = HRESULT_FROM_WIN32(lRet);
1523 if (SUCCEEDED(hr))
1525 lRet = RegSetValueExW(hPinsKey, wszIsRendered, 0, REG_DWORD, (LPBYTE)&bRendered, sizeof(bRendered));
1526 hr = HRESULT_FROM_WIN32(lRet);
1529 if (SUCCEEDED(hr))
1531 HKEY hkeyDummy = NULL;
1533 lRet = RegCreateKeyExW(hPinsKey, wszTypes, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1534 hr = HRESULT_FROM_WIN32(lRet);
1536 if (hkeyDummy) RegCloseKey(hkeyDummy);
1539 CoTaskMemFree(wszClsid);
1540 if (hKey)
1541 CloseHandle(hKey);
1542 if (hPinsKey)
1543 CloseHandle(hPinsKey);
1545 return hr;
1549 static HRESULT WINAPI FilterMapper_RegisterPinType(
1550 IFilterMapper * iface,
1551 CLSID clsFilter,
1552 LPCWSTR szName,
1553 CLSID clsMajorType,
1554 CLSID clsSubType)
1556 HRESULT hr;
1557 LONG lRet;
1558 LPWSTR wszClsid = NULL;
1559 LPWSTR wszClsidMajorType = NULL;
1560 LPWSTR wszClsidSubType = NULL;
1561 HKEY hKey = NULL;
1562 WCHAR * wszTypesKey;
1563 WCHAR wszKeyName[MAX_PATH];
1565 TRACE("(%p)->(%s, %s, %s, %s)\n", iface, debugstr_guid(&clsFilter), debugstr_w(szName),
1566 debugstr_guid(&clsMajorType), debugstr_guid(&clsSubType));
1568 hr = StringFromCLSID(&clsFilter, &wszClsid);
1570 if (SUCCEEDED(hr))
1572 hr = StringFromCLSID(&clsMajorType, &wszClsidMajorType);
1575 if (SUCCEEDED(hr))
1577 hr = StringFromCLSID(&clsSubType, &wszClsidSubType);
1580 if (SUCCEEDED(hr))
1582 wszTypesKey = CoTaskMemAlloc((strlenW(wszClsidSlash) + strlenW(wszClsid) + strlenW(wszPins) +
1583 strlenW(szName) + strlenW(wszTypes) + 3 + 1) * 2);
1584 if (!wszTypesKey)
1585 hr = E_OUTOFMEMORY;
1588 if (SUCCEEDED(hr))
1590 strcpyW(wszTypesKey, wszClsidSlash);
1591 strcatW(wszTypesKey, wszClsid);
1592 strcatW(wszTypesKey, wszSlash);
1593 strcatW(wszTypesKey, wszPins);
1594 strcatW(wszTypesKey, wszSlash);
1595 strcatW(wszTypesKey, szName);
1596 strcatW(wszTypesKey, wszSlash);
1597 strcatW(wszTypesKey, wszTypes);
1599 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszTypesKey, 0, KEY_WRITE, &hKey);
1600 hr = HRESULT_FROM_WIN32(lRet);
1601 CoTaskMemFree(wszTypesKey);
1604 if (SUCCEEDED(hr))
1606 HKEY hkeyDummy = NULL;
1608 strcpyW(wszKeyName, wszClsidMajorType);
1609 strcatW(wszKeyName, wszSlash);
1610 strcatW(wszKeyName, wszClsidSubType);
1612 lRet = RegCreateKeyExW(hKey, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1613 hr = HRESULT_FROM_WIN32(lRet);
1614 CloseHandle(hKey);
1616 if (hkeyDummy) RegCloseKey(hkeyDummy);
1619 CoTaskMemFree(wszClsid);
1620 CoTaskMemFree(wszClsidMajorType);
1621 CoTaskMemFree(wszClsidSubType);
1623 return hr;
1626 static HRESULT WINAPI FilterMapper_UnregisterFilter(IFilterMapper * iface, CLSID Filter)
1628 HRESULT hr;
1629 LONG lRet;
1630 LPWSTR wszClsid = NULL;
1631 HKEY hKey;
1632 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1634 TRACE("(%p)->(%s)\n", iface, debugstr_guid(&Filter));
1636 hr = StringFromCLSID(&Filter, &wszClsid);
1638 if (SUCCEEDED(hr))
1640 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilter, 0, KEY_WRITE, &hKey);
1641 hr = HRESULT_FROM_WIN32(lRet);
1644 if (SUCCEEDED(hr))
1646 lRet = RegDeleteKeyW(hKey, wszClsid);
1647 hr = HRESULT_FROM_WIN32(lRet);
1648 CloseHandle(hKey);
1651 if (SUCCEEDED(hr))
1653 strcpyW(wszKeyName, wszClsidSlash);
1654 strcatW(wszKeyName, wszClsid);
1656 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1657 hr = HRESULT_FROM_WIN32(lRet);
1660 if (SUCCEEDED(hr))
1662 lRet = RegDeleteValueW(hKey, wszMeritName);
1663 if (lRet != ERROR_SUCCESS)
1664 hr = HRESULT_FROM_WIN32(lRet);
1666 lRet = RegDeleteTreeW(hKey, wszPins);
1667 if (lRet != ERROR_SUCCESS)
1668 hr = HRESULT_FROM_WIN32(lRet);
1670 CloseHandle(hKey);
1673 CoTaskMemFree(wszClsid);
1675 return hr;
1678 static HRESULT WINAPI FilterMapper_UnregisterFilterInstance(IFilterMapper * iface, CLSID MRId)
1680 TRACE("(%p)->(%s)\n", iface, debugstr_guid(&MRId));
1682 /* Not implemented in Windows (tested on Win2k) */
1684 return E_NOTIMPL;
1687 static HRESULT WINAPI FilterMapper_UnregisterPin(IFilterMapper * iface, CLSID Filter, LPCWSTR Name)
1689 HRESULT hr;
1690 LONG lRet;
1691 LPWSTR wszClsid = NULL;
1692 HKEY hKey = NULL;
1693 WCHAR * wszPinNameKey;
1694 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1696 TRACE("(%p)->(%s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(Name));
1698 if (!Name)
1699 return E_INVALIDARG;
1701 hr = StringFromCLSID(&Filter, &wszClsid);
1703 if (SUCCEEDED(hr))
1705 strcpyW(wszKeyName, wszClsidSlash);
1706 strcatW(wszKeyName, wszClsid);
1708 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1709 hr = HRESULT_FROM_WIN32(lRet);
1712 if (SUCCEEDED(hr))
1714 wszPinNameKey = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(Name) + 1) * 2);
1715 if (!wszPinNameKey)
1716 hr = E_OUTOFMEMORY;
1719 if (SUCCEEDED(hr))
1721 strcpyW(wszPinNameKey, wszPins);
1722 strcatW(wszPinNameKey, wszSlash);
1723 strcatW(wszPinNameKey, Name);
1725 lRet = RegDeleteTreeW(hKey, wszPinNameKey);
1726 hr = HRESULT_FROM_WIN32(lRet);
1727 CoTaskMemFree(wszPinNameKey);
1730 CoTaskMemFree(wszClsid);
1731 if (hKey)
1732 CloseHandle(hKey);
1734 return hr;
1737 static const IFilterMapperVtbl fmvtbl =
1740 FilterMapper_QueryInterface,
1741 FilterMapper_AddRef,
1742 FilterMapper_Release,
1744 FilterMapper_RegisterFilter,
1745 FilterMapper_RegisterFilterInstance,
1746 FilterMapper_RegisterPin,
1747 FilterMapper_RegisterPinType,
1748 FilterMapper_UnregisterFilter,
1749 FilterMapper_UnregisterFilterInstance,
1750 FilterMapper_UnregisterPin,
1751 FilterMapper_EnumMatchingFilters
1755 /*** IUnknown methods ***/
1756 static HRESULT WINAPI AMFilterData_QueryInterface(IAMFilterData * iface, REFIID riid, LPVOID *ppv)
1758 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1760 return FilterMapper3_QueryInterface((IFilterMapper3*)This, riid, ppv);
1763 static ULONG WINAPI AMFilterData_AddRef(IAMFilterData * iface)
1765 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1767 return FilterMapper3_AddRef((IFilterMapper3*)This);
1770 static ULONG WINAPI AMFilterData_Release(IAMFilterData * iface)
1772 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1774 return FilterMapper3_Release((IFilterMapper3*)This);
1777 /*** IAMFilterData methods ***/
1778 static HRESULT WINAPI AMFilterData_ParseFilterData(IAMFilterData* iface,
1779 BYTE *pData, ULONG cb,
1780 BYTE **ppRegFilter2)
1782 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1783 HRESULT hr = S_OK;
1784 REGFILTER2 *prf2;
1786 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pData, cb, ppRegFilter2);
1788 prf2 = CoTaskMemAlloc(sizeof(*prf2));
1789 if (!prf2)
1790 return E_OUTOFMEMORY;
1791 *ppRegFilter2 = (BYTE *)prf2;
1793 hr = FM2_ReadFilterData(pData, prf2);
1794 if (FAILED(hr))
1796 CoTaskMemFree(prf2);
1797 *ppRegFilter2 = NULL;
1800 return hr;
1803 static HRESULT WINAPI AMFilterData_CreateFilterData(IAMFilterData* iface,
1804 REGFILTER2 *prf2,
1805 BYTE **pRegFilterData,
1806 ULONG *pcb)
1808 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1810 TRACE("(%p/%p)->(%p, %p, %p)\n", This, iface, prf2, pRegFilterData, pcb);
1812 return FM2_WriteFilterData(prf2, pRegFilterData, pcb);
1815 static const IAMFilterDataVtbl AMFilterDataVtbl = {
1816 AMFilterData_QueryInterface,
1817 AMFilterData_AddRef,
1818 AMFilterData_Release,
1819 AMFilterData_ParseFilterData,
1820 AMFilterData_CreateFilterData