d2d1: Add a pixel shader for triangle/solid/bitmap draws.
[wine.git] / dlls / quartz / filtermapper.c
bloba462fbcdde980cc99473bc56c950b4b4fcd55be1
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"
39 #include "initguid.h"
40 #include "fil_data.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
46 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
48 typedef struct FilterMapper3Impl
50 IUnknown IUnknown_inner;
51 IFilterMapper3 IFilterMapper3_iface;
52 IFilterMapper IFilterMapper_iface;
53 IAMFilterData IAMFilterData_iface;
54 IUnknown *outer_unk;
55 LONG ref;
56 } FilterMapper3Impl;
58 static inline FilterMapper3Impl *impl_from_IFilterMapper3( IFilterMapper3 *iface )
60 return CONTAINING_RECORD(iface, FilterMapper3Impl, IFilterMapper3_iface);
63 static inline FilterMapper3Impl *impl_from_IFilterMapper( IFilterMapper *iface )
65 return CONTAINING_RECORD(iface, FilterMapper3Impl, IFilterMapper_iface);
68 static inline FilterMapper3Impl *impl_from_IAMFilterData( IAMFilterData *iface )
70 return CONTAINING_RECORD(iface, FilterMapper3Impl, IAMFilterData_iface);
73 static inline FilterMapper3Impl *impl_from_IUnknown( IUnknown *iface )
75 return CONTAINING_RECORD(iface, FilterMapper3Impl, IUnknown_inner);
78 static const WCHAR wszClsidSlash[] = {'C','L','S','I','D','\\',0};
79 static const WCHAR wszSlashInstance[] = {'\\','I','n','s','t','a','n','c','e','\\',0};
80 static const WCHAR wszSlash[] = {'\\',0};
82 /* CLSID property in media category Moniker */
83 static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
84 /* FriendlyName property in media category Moniker */
85 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
86 /* Merit property in media category Moniker (CLSID_ActiveMovieCategories only) */
87 static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
88 /* FilterData property in media category Moniker (not CLSID_ActiveMovieCategories) */
89 static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
90 /* For filters registered with IFilterMapper */
91 static const WCHAR wszFilterSlash[] = {'F','i','l','t','e','r','\\',0};
92 static const WCHAR wszFilter[] = {'F','i','l','t','e','r',0};
93 /* For pins registered with IFilterMapper */
94 static const WCHAR wszPins[] = {'P','i','n','s',0};
95 static const WCHAR wszAllowedMany[] = {'A','l','l','o','w','e','d','M','a','n','y',0};
96 static const WCHAR wszAllowedZero[] = {'A','l','l','o','w','e','d','Z','e','r','o',0};
97 static const WCHAR wszDirection[] = {'D','i','r','e','c','t','i','o','n',0};
98 static const WCHAR wszIsRendered[] = {'I','s','R','e','n','d','e','r','e','d',0};
99 /* For types registered with IFilterMapper */
100 static const WCHAR wszTypes[] = {'T','y','p','e','s',0};
103 /* registry format for REGFILTER2 */
104 struct REG_RF
106 DWORD dwVersion;
107 DWORD dwMerit;
108 DWORD dwPins;
109 DWORD dwUnused;
112 struct REG_RFP
114 BYTE signature[4]; /* e.g. "0pi3" */
115 DWORD dwFlags;
116 DWORD dwInstances;
117 DWORD dwMediaTypes;
118 DWORD dwMediums;
119 DWORD bCategory; /* is there a category clsid? */
120 /* optional: dwOffsetCategoryClsid */
123 struct REG_TYPE
125 BYTE signature[4]; /* e.g. "0ty3" */
126 DWORD dwUnused;
127 DWORD dwOffsetMajor;
128 DWORD dwOffsetMinor;
131 struct MONIKER_MERIT
133 IMoniker * pMoniker;
134 DWORD dwMerit;
137 struct Vector
139 LPBYTE pData;
140 int capacity; /* in bytes */
141 int current; /* pointer to next free byte */
144 /* returns the position it was added at */
145 static int add_data(struct Vector * v, const BYTE * pData, int size)
147 int index = v->current;
148 if (v->current + size > v->capacity)
150 LPBYTE pOldData = v->pData;
151 v->capacity = (v->capacity + size) * 2;
152 v->pData = CoTaskMemAlloc(v->capacity);
153 memcpy(v->pData, pOldData, v->current);
154 CoTaskMemFree(pOldData);
156 memcpy(v->pData + v->current, pData, size);
157 v->current += size;
158 return index;
161 static int find_data(const struct Vector * v, const BYTE * pData, int size)
163 int index;
164 for (index = 0; index < v->current; index++)
165 if (!memcmp(v->pData + index, pData, size))
166 return index;
167 /* not found */
168 return -1;
171 static void delete_vector(struct Vector * v)
173 CoTaskMemFree(v->pData);
174 v->current = 0;
175 v->capacity = 0;
178 /*** IUnknown (inner) methods ***/
180 static HRESULT WINAPI Inner_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
182 FilterMapper3Impl *This = impl_from_IUnknown(iface);
184 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
186 *ppv = NULL;
187 if (IsEqualIID(riid, &IID_IUnknown))
188 *ppv = &This->IUnknown_inner;
189 else if (IsEqualIID(riid, &IID_IFilterMapper2) || IsEqualIID(riid, &IID_IFilterMapper3))
190 *ppv = &This->IFilterMapper3_iface;
191 else if (IsEqualIID(riid, &IID_IFilterMapper))
192 *ppv = &This->IFilterMapper_iface;
193 else if (IsEqualIID(riid, &IID_IAMFilterData))
194 *ppv = &This->IAMFilterData_iface;
196 if (*ppv != NULL)
198 IUnknown_AddRef((IUnknown *)*ppv);
199 return S_OK;
202 FIXME("No interface for %s\n", debugstr_guid(riid));
203 return E_NOINTERFACE;
206 static ULONG WINAPI Inner_AddRef(IUnknown *iface)
208 FilterMapper3Impl *This = impl_from_IUnknown(iface);
209 ULONG ref = InterlockedIncrement(&This->ref);
211 TRACE("(%p)->(): new ref = %d\n", This, ref);
213 return ref;
216 static ULONG WINAPI Inner_Release(IUnknown *iface)
218 FilterMapper3Impl *This = impl_from_IUnknown(iface);
219 ULONG ref = InterlockedDecrement(&This->ref);
221 TRACE("(%p)->(): new ref = %d\n", This, ref);
223 if (ref == 0)
224 CoTaskMemFree(This);
226 return ref;
229 static const IUnknownVtbl IInner_VTable =
231 Inner_QueryInterface,
232 Inner_AddRef,
233 Inner_Release
236 static HRESULT WINAPI FilterMapper3_QueryInterface(IFilterMapper3 * iface, REFIID riid, LPVOID *ppv)
238 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
240 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
243 static ULONG WINAPI FilterMapper3_AddRef(IFilterMapper3 * iface)
245 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
247 return IUnknown_AddRef(This->outer_unk);
250 static ULONG WINAPI FilterMapper3_Release(IFilterMapper3 * iface)
252 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
254 return IUnknown_Release(This->outer_unk);
257 /*** IFilterMapper3 methods ***/
259 static HRESULT WINAPI FilterMapper3_CreateCategory(
260 IFilterMapper3 * iface,
261 REFCLSID clsidCategory,
262 DWORD dwCategoryMerit,
263 LPCWSTR szDescription)
265 LPWSTR wClsidAMCat = NULL;
266 LPWSTR wClsidCategory = NULL;
267 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + ARRAYSIZE(wszSlashInstance)-1 + (CHARS_IN_GUID-1) * 2 + 1];
268 HKEY hKey = NULL;
269 LONG lRet;
270 HRESULT hr;
272 TRACE("(%s, %x, %s)\n", debugstr_guid(clsidCategory), dwCategoryMerit, debugstr_w(szDescription));
274 hr = StringFromCLSID(&CLSID_ActiveMovieCategories, &wClsidAMCat);
276 if (SUCCEEDED(hr))
278 hr = StringFromCLSID(clsidCategory, &wClsidCategory);
281 if (SUCCEEDED(hr))
283 strcpyW(wszKeyName, wszClsidSlash);
284 strcatW(wszKeyName, wClsidAMCat);
285 strcatW(wszKeyName, wszSlashInstance);
286 strcatW(wszKeyName, wClsidCategory);
288 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
289 hr = HRESULT_FROM_WIN32(lRet);
292 if (SUCCEEDED(hr))
294 lRet = RegSetValueExW(hKey, wszFriendlyName, 0, REG_SZ, (const BYTE*)szDescription, (strlenW(szDescription) + 1) * sizeof(WCHAR));
295 hr = HRESULT_FROM_WIN32(lRet);
298 if (SUCCEEDED(hr))
300 lRet = RegSetValueExW(hKey, wszClsidName, 0, REG_SZ, (LPBYTE)wClsidCategory, (strlenW(wClsidCategory) + 1) * sizeof(WCHAR));
301 hr = HRESULT_FROM_WIN32(lRet);
304 if (SUCCEEDED(hr))
306 lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwCategoryMerit, sizeof(dwCategoryMerit));
307 hr = HRESULT_FROM_WIN32(lRet);
310 RegCloseKey(hKey);
311 CoTaskMemFree(wClsidCategory);
312 CoTaskMemFree(wClsidAMCat);
314 return hr;
317 static HRESULT WINAPI FilterMapper3_UnregisterFilter(
318 IFilterMapper3 * iface,
319 const CLSID *pclsidCategory,
320 const OLECHAR *szInstance,
321 REFCLSID Filter)
323 WCHAR wszKeyName[MAX_PATH];
324 LPWSTR wClsidCategory = NULL;
325 LPWSTR wFilter = NULL;
326 HRESULT hr;
328 TRACE("(%p, %s, %s)\n", pclsidCategory, debugstr_w(szInstance), debugstr_guid(Filter));
330 if (!pclsidCategory)
331 pclsidCategory = &CLSID_LegacyAmFilterCategory;
333 hr = StringFromCLSID(pclsidCategory, &wClsidCategory);
335 if (SUCCEEDED(hr))
337 strcpyW(wszKeyName, wszClsidSlash);
338 strcatW(wszKeyName, wClsidCategory);
339 strcatW(wszKeyName, wszSlashInstance);
340 if (szInstance)
341 strcatW(wszKeyName, szInstance);
342 else
344 hr = StringFromCLSID(Filter, &wFilter);
345 if (SUCCEEDED(hr))
346 strcatW(wszKeyName, wFilter);
350 if (SUCCEEDED(hr))
352 LONG lRet = RegDeleteKeyW(HKEY_CLASSES_ROOT, wszKeyName);
353 hr = HRESULT_FROM_WIN32(lRet);
356 CoTaskMemFree(wClsidCategory);
357 CoTaskMemFree(wFilter);
359 return hr;
362 static HRESULT FM2_WriteFriendlyName(IPropertyBag * pPropBag, LPCWSTR szName)
364 VARIANT var;
365 HRESULT ret;
366 BSTR value;
368 V_VT(&var) = VT_BSTR;
369 V_BSTR(&var) = value = SysAllocString(szName);
371 ret = IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
372 SysFreeString(value);
374 return ret;
377 static HRESULT FM2_WriteClsid(IPropertyBag * pPropBag, REFCLSID clsid)
379 LPWSTR wszClsid = NULL;
380 VARIANT var;
381 HRESULT hr;
383 hr = StringFromCLSID(clsid, &wszClsid);
385 if (SUCCEEDED(hr))
387 V_VT(&var) = VT_BSTR;
388 V_BSTR(&var) = wszClsid;
389 hr = IPropertyBag_Write(pPropBag, wszClsidName, &var);
391 CoTaskMemFree(wszClsid);
392 return hr;
395 static HRESULT FM2_WriteFilterData(const REGFILTER2 * prf2, BYTE **ppData, ULONG *pcbData)
397 int size = sizeof(struct REG_RF);
398 unsigned int i;
399 struct Vector mainStore = {NULL, 0, 0};
400 struct Vector clsidStore = {NULL, 0, 0};
401 struct REG_RF rrf;
402 HRESULT hr = S_OK;
404 rrf.dwVersion = prf2->dwVersion;
405 rrf.dwMerit = prf2->dwMerit;
406 rrf.dwPins = prf2->u.s2.cPins2;
407 rrf.dwUnused = 0;
409 add_data(&mainStore, (LPBYTE)&rrf, sizeof(rrf));
411 for (i = 0; i < prf2->u.s2.cPins2; i++)
413 size += sizeof(struct REG_RFP);
414 if (prf2->u.s2.rgPins2[i].clsPinCategory)
415 size += sizeof(DWORD);
416 size += prf2->u.s2.rgPins2[i].nMediaTypes * sizeof(struct REG_TYPE);
417 size += prf2->u.s2.rgPins2[i].nMediums * sizeof(DWORD);
420 for (i = 0; i < prf2->u.s2.cPins2; i++)
422 struct REG_RFP rrfp;
423 REGFILTERPINS2 rgPin2 = prf2->u.s2.rgPins2[i];
424 unsigned int j;
426 rrfp.signature[0] = '0';
427 rrfp.signature[1] = 'p';
428 rrfp.signature[2] = 'i';
429 rrfp.signature[3] = '3';
430 rrfp.signature[0] += i;
431 rrfp.dwFlags = rgPin2.dwFlags;
432 rrfp.dwInstances = rgPin2.cInstances;
433 rrfp.dwMediaTypes = rgPin2.nMediaTypes;
434 rrfp.dwMediums = rgPin2.nMediums;
435 rrfp.bCategory = rgPin2.clsPinCategory ? 1 : 0;
437 add_data(&mainStore, (LPBYTE)&rrfp, sizeof(rrfp));
438 if (rrfp.bCategory)
440 DWORD index = find_data(&clsidStore, (const BYTE*)rgPin2.clsPinCategory, sizeof(CLSID));
441 if (index == -1)
442 index = add_data(&clsidStore, (const BYTE*)rgPin2.clsPinCategory, sizeof(CLSID));
443 index += size;
445 add_data(&mainStore, (LPBYTE)&index, sizeof(index));
448 for (j = 0; j < rgPin2.nMediaTypes; j++)
450 struct REG_TYPE rt;
451 const CLSID * clsMinorType = rgPin2.lpMediaType[j].clsMinorType ? rgPin2.lpMediaType[j].clsMinorType : &MEDIASUBTYPE_NULL;
452 rt.signature[0] = '0';
453 rt.signature[1] = 't';
454 rt.signature[2] = 'y';
455 rt.signature[3] = '3';
456 rt.signature[0] += j;
457 rt.dwUnused = 0;
458 rt.dwOffsetMajor = find_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
459 if (rt.dwOffsetMajor == -1)
460 rt.dwOffsetMajor = add_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
461 rt.dwOffsetMajor += size;
462 rt.dwOffsetMinor = find_data(&clsidStore, (const BYTE*)clsMinorType, sizeof(CLSID));
463 if (rt.dwOffsetMinor == -1)
464 rt.dwOffsetMinor = add_data(&clsidStore, (const BYTE*)clsMinorType, sizeof(CLSID));
465 rt.dwOffsetMinor += size;
467 add_data(&mainStore, (LPBYTE)&rt, sizeof(rt));
470 for (j = 0; j < rgPin2.nMediums; j++)
472 DWORD index = find_data(&clsidStore, (const BYTE*)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
473 if (index == -1)
474 index = add_data(&clsidStore, (const BYTE*)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
475 index += size;
477 add_data(&mainStore, (LPBYTE)&index, sizeof(index));
481 if (SUCCEEDED(hr))
483 *pcbData = mainStore.current + clsidStore.current;
484 *ppData = CoTaskMemAlloc(*pcbData);
485 if (!*ppData)
486 hr = E_OUTOFMEMORY;
489 if (SUCCEEDED(hr))
491 memcpy(*ppData, mainStore.pData, mainStore.current);
492 memcpy((*ppData) + mainStore.current, clsidStore.pData, clsidStore.current);
495 delete_vector(&mainStore);
496 delete_vector(&clsidStore);
497 return hr;
500 static HRESULT FM2_ReadFilterData(BYTE *pData, REGFILTER2 * prf2)
502 HRESULT hr = S_OK;
503 struct REG_RF * prrf;
504 LPBYTE pCurrent;
505 DWORD i;
506 REGFILTERPINS2 * rgPins2;
508 prrf = (struct REG_RF *)pData;
509 pCurrent = pData;
511 if (prrf->dwVersion != 2)
513 FIXME("Filter registry version %d not supported\n", prrf->dwVersion);
514 ZeroMemory(prf2, sizeof(*prf2));
515 hr = E_FAIL;
518 if (SUCCEEDED(hr))
520 TRACE("version = %d, merit = %x, #pins = %d, unused = %x\n",
521 prrf->dwVersion, prrf->dwMerit, prrf->dwPins, prrf->dwUnused);
523 prf2->dwVersion = prrf->dwVersion;
524 prf2->dwMerit = prrf->dwMerit;
525 prf2->u.s2.cPins2 = prrf->dwPins;
526 rgPins2 = CoTaskMemAlloc(prrf->dwPins * sizeof(*rgPins2));
527 prf2->u.s2.rgPins2 = rgPins2;
528 pCurrent += sizeof(struct REG_RF);
530 for (i = 0; i < prrf->dwPins; i++)
532 struct REG_RFP * prrfp = (struct REG_RFP *)pCurrent;
533 REGPINTYPES * lpMediaType;
534 REGPINMEDIUM * lpMedium;
535 UINT j;
537 /* FIXME: check signature */
539 TRACE("\tsignature = %s\n", debugstr_an((const char*)prrfp->signature, 4));
541 TRACE("\tpin[%d]: flags = %x, instances = %d, media types = %d, mediums = %d\n",
542 i, prrfp->dwFlags, prrfp->dwInstances, prrfp->dwMediaTypes, prrfp->dwMediums);
544 rgPins2[i].dwFlags = prrfp->dwFlags;
545 rgPins2[i].cInstances = prrfp->dwInstances;
546 rgPins2[i].nMediaTypes = prrfp->dwMediaTypes;
547 rgPins2[i].nMediums = prrfp->dwMediums;
548 pCurrent += sizeof(struct REG_RFP);
549 if (prrfp->bCategory)
551 CLSID * clsCat = CoTaskMemAlloc(sizeof(CLSID));
552 memcpy(clsCat, pData + *(DWORD*)(pCurrent), sizeof(CLSID));
553 pCurrent += sizeof(DWORD);
554 rgPins2[i].clsPinCategory = clsCat;
556 else
557 rgPins2[i].clsPinCategory = NULL;
559 if (rgPins2[i].nMediaTypes > 0)
560 lpMediaType = CoTaskMemAlloc(rgPins2[i].nMediaTypes * sizeof(*lpMediaType));
561 else
562 lpMediaType = NULL;
564 rgPins2[i].lpMediaType = lpMediaType;
566 for (j = 0; j < rgPins2[i].nMediaTypes; j++)
568 struct REG_TYPE * prt = (struct REG_TYPE *)pCurrent;
569 CLSID * clsMajor = CoTaskMemAlloc(sizeof(CLSID));
570 CLSID * clsMinor = CoTaskMemAlloc(sizeof(CLSID));
572 /* FIXME: check signature */
573 TRACE("\t\tsignature = %s\n", debugstr_an((const char*)prt->signature, 4));
575 memcpy(clsMajor, pData + prt->dwOffsetMajor, sizeof(CLSID));
576 memcpy(clsMinor, pData + prt->dwOffsetMinor, sizeof(CLSID));
578 lpMediaType[j].clsMajorType = clsMajor;
579 lpMediaType[j].clsMinorType = clsMinor;
581 pCurrent += sizeof(*prt);
584 if (rgPins2[i].nMediums > 0)
585 lpMedium = CoTaskMemAlloc(rgPins2[i].nMediums * sizeof(*lpMedium));
586 else
587 lpMedium = NULL;
589 rgPins2[i].lpMedium = lpMedium;
591 for (j = 0; j < rgPins2[i].nMediums; j++)
593 DWORD dwOffset = *(DWORD *)pCurrent;
595 memcpy(lpMedium + j, pData + dwOffset, sizeof(REGPINMEDIUM));
597 pCurrent += sizeof(dwOffset);
603 return hr;
606 static void FM2_DeleteRegFilter(REGFILTER2 * prf2)
608 UINT i;
609 for (i = 0; i < prf2->u.s2.cPins2; i++)
611 UINT j;
612 if (prf2->u.s2.rgPins2[i].clsPinCategory)
613 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].clsPinCategory);
615 for (j = 0; j < prf2->u.s2.rgPins2[i].nMediaTypes; j++)
617 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMediaType[j].clsMajorType);
618 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMediaType[j].clsMinorType);
620 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMediaType);
621 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMedium);
623 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2);
626 static HRESULT WINAPI FilterMapper3_RegisterFilter(
627 IFilterMapper3 * iface,
628 REFCLSID clsidFilter,
629 LPCWSTR szName,
630 IMoniker **ppMoniker,
631 const CLSID *pclsidCategory,
632 const OLECHAR *szInstance,
633 const REGFILTER2 *prf2)
635 IParseDisplayName * pParser = NULL;
636 IBindCtx * pBindCtx = NULL;
637 IMoniker * pMoniker = NULL;
638 IPropertyBag * pPropBag = NULL;
639 HRESULT hr;
640 LPWSTR pwszParseName = NULL;
641 LPWSTR pCurrent;
642 static const WCHAR wszDevice[] = {'@','d','e','v','i','c','e',':','s','w',':',0};
643 int nameLen;
644 ULONG ulEaten;
645 LPWSTR szClsidTemp = NULL;
646 REGFILTER2 regfilter2;
647 REGFILTERPINS2* pregfp2 = NULL;
649 TRACE("(%s, %s, %p, %s, %s, %p)\n",
650 debugstr_guid(clsidFilter),
651 debugstr_w(szName),
652 ppMoniker,
653 debugstr_guid(pclsidCategory),
654 debugstr_w(szInstance),
655 prf2);
657 if (prf2->dwVersion == 2)
659 regfilter2 = *prf2;
661 else if (prf2->dwVersion == 1)
663 ULONG i;
664 DWORD flags;
665 /* REGFILTER2 structure is converted from version 1 to 2. Tested on Win2k. */
666 regfilter2.dwVersion = 2;
667 regfilter2.dwMerit = prf2->dwMerit;
668 regfilter2.u.s2.cPins2 = prf2->u.s1.cPins;
669 pregfp2 = CoTaskMemAlloc(prf2->u.s1.cPins * sizeof(REGFILTERPINS2));
670 regfilter2.u.s2.rgPins2 = pregfp2;
671 for (i = 0; i < prf2->u.s1.cPins; i++)
673 flags = 0;
674 if (prf2->u.s1.rgPins[i].bRendered)
675 flags |= REG_PINFLAG_B_RENDERER;
676 if (prf2->u.s1.rgPins[i].bOutput)
677 flags |= REG_PINFLAG_B_OUTPUT;
678 if (prf2->u.s1.rgPins[i].bZero)
679 flags |= REG_PINFLAG_B_ZERO;
680 if (prf2->u.s1.rgPins[i].bMany)
681 flags |= REG_PINFLAG_B_MANY;
682 pregfp2[i].dwFlags = flags;
683 pregfp2[i].cInstances = 1;
684 pregfp2[i].nMediaTypes = prf2->u.s1.rgPins[i].nMediaTypes;
685 pregfp2[i].lpMediaType = prf2->u.s1.rgPins[i].lpMediaType;
686 pregfp2[i].nMediums = 0;
687 pregfp2[i].lpMedium = NULL;
688 pregfp2[i].clsPinCategory = NULL;
691 else
693 FIXME("dwVersion other that 1 or 2 not supported at the moment\n");
694 return E_NOTIMPL;
697 if (ppMoniker)
698 *ppMoniker = NULL;
700 if (!pclsidCategory)
701 /* MSDN mentions the inexistent CLSID_ActiveMovieFilters GUID.
702 * In fact this is the CLSID_LegacyAmFilterCategory one */
703 pclsidCategory = &CLSID_LegacyAmFilterCategory;
705 /* sizeof... will include the null terminator and
706 * the + 1 is for the separator ('\\'). The -1 is
707 * because CHARS_IN_GUID includes the null terminator
709 nameLen = sizeof(wszDevice)/sizeof(wszDevice[0]) + CHARS_IN_GUID - 1 + 1;
711 if (szInstance)
712 nameLen += strlenW(szInstance);
713 else
714 nameLen += CHARS_IN_GUID - 1; /* CHARS_IN_GUID includes null terminator */
716 pCurrent = pwszParseName = CoTaskMemAlloc(nameLen*sizeof(WCHAR));
717 if (!pwszParseName)
718 return E_OUTOFMEMORY;
720 strcpyW(pwszParseName, wszDevice);
721 pCurrent += strlenW(wszDevice);
723 hr = StringFromCLSID(pclsidCategory, &szClsidTemp);
725 if (SUCCEEDED(hr))
727 memcpy(pCurrent, szClsidTemp, CHARS_IN_GUID * sizeof(WCHAR));
728 pCurrent += CHARS_IN_GUID - 1;
729 pCurrent[0] = '\\';
731 if (szInstance)
732 strcpyW(pCurrent+1, szInstance);
733 else
735 CoTaskMemFree(szClsidTemp);
736 szClsidTemp = NULL;
738 hr = StringFromCLSID(clsidFilter, &szClsidTemp);
739 if (SUCCEEDED(hr))
740 strcpyW(pCurrent+1, szClsidTemp);
744 if (SUCCEEDED(hr))
745 hr = CoCreateInstance(&CLSID_CDeviceMoniker, NULL, CLSCTX_INPROC, &IID_IParseDisplayName, (LPVOID *)&pParser);
747 if (SUCCEEDED(hr))
748 hr = CreateBindCtx(0, &pBindCtx);
750 if (SUCCEEDED(hr))
751 hr = IParseDisplayName_ParseDisplayName(pParser, pBindCtx, pwszParseName, &ulEaten, &pMoniker);
753 if (pBindCtx)
754 IBindCtx_Release(pBindCtx);
755 if (pParser)
756 IParseDisplayName_Release(pParser);
758 if (SUCCEEDED(hr))
759 hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
761 if (SUCCEEDED(hr))
762 hr = FM2_WriteFriendlyName(pPropBag, szName);
764 if (SUCCEEDED(hr))
765 hr = FM2_WriteClsid(pPropBag, clsidFilter);
767 if (SUCCEEDED(hr))
769 BYTE *pData;
770 ULONG cbData;
772 hr = FM2_WriteFilterData(&regfilter2, &pData, &cbData);
773 if (SUCCEEDED(hr))
775 VARIANT var;
776 SAFEARRAY *psa;
777 SAFEARRAYBOUND saBound;
779 saBound.lLbound = 0;
780 saBound.cElements = cbData;
781 psa = SafeArrayCreate(VT_UI1, 1, &saBound);
782 if (!psa)
784 ERR("Couldn't create SAFEARRAY\n");
785 hr = E_FAIL;
788 if (SUCCEEDED(hr))
790 LPBYTE pbSAData;
791 hr = SafeArrayAccessData(psa, (LPVOID *)&pbSAData);
792 if (SUCCEEDED(hr))
794 memcpy(pbSAData, pData, cbData);
795 hr = SafeArrayUnaccessData(psa);
799 V_VT(&var) = VT_ARRAY | VT_UI1;
800 V_ARRAY(&var) = psa;
802 if (SUCCEEDED(hr))
803 hr = IPropertyBag_Write(pPropBag, wszFilterDataName, &var);
805 if (psa)
806 SafeArrayDestroy(psa);
807 CoTaskMemFree(pData);
811 if (pPropBag)
812 IPropertyBag_Release(pPropBag);
813 CoTaskMemFree(szClsidTemp);
814 CoTaskMemFree(pwszParseName);
816 if (SUCCEEDED(hr) && ppMoniker)
817 *ppMoniker = pMoniker;
818 else if (pMoniker)
819 IMoniker_Release(pMoniker);
821 CoTaskMemFree(pregfp2);
823 TRACE("-- returning %x\n", hr);
825 return hr;
828 /* internal helper function */
829 static BOOL MatchTypes(
830 BOOL bExactMatch,
831 DWORD nPinTypes,
832 const REGPINTYPES * pPinTypes,
833 DWORD nMatchTypes,
834 const GUID * pMatchTypes)
836 BOOL bMatch = FALSE;
837 DWORD j;
839 if ((nMatchTypes == 0) && (nPinTypes > 0))
840 bMatch = TRUE;
842 for (j = 0; j < nPinTypes; j++)
844 DWORD i;
845 for (i = 0; i < nMatchTypes*2; i+=2)
847 if (((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMajorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMajorType, &pMatchTypes[i])) &&
848 ((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMinorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i+1], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMinorType, &pMatchTypes[i+1])))
850 bMatch = TRUE;
851 break;
855 return bMatch;
858 /* internal helper function for qsort of MONIKER_MERIT array */
859 static int mm_compare(const void * left, const void * right)
861 const struct MONIKER_MERIT * mmLeft = left;
862 const struct MONIKER_MERIT * mmRight = right;
864 if (mmLeft->dwMerit == mmRight->dwMerit)
865 return 0;
866 if (mmLeft->dwMerit > mmRight->dwMerit)
867 return -1;
868 return 1;
871 /* NOTES:
872 * Exact match means whether or not to treat GUID_NULL's in filter data as wild cards
873 * (GUID_NULL's in input to function automatically treated as wild cards)
874 * Input/Output needed means match only on criteria if TRUE (with zero input types
875 * meaning match any input/output pin as long as one exists), otherwise match any
876 * filter that meets the rest of the requirements.
878 static HRESULT WINAPI FilterMapper3_EnumMatchingFilters(
879 IFilterMapper3 * iface,
880 IEnumMoniker **ppEnum,
881 DWORD dwFlags,
882 BOOL bExactMatch,
883 DWORD dwMerit,
884 BOOL bInputNeeded,
885 DWORD cInputTypes,
886 const GUID *pInputTypes,
887 const REGPINMEDIUM *pMedIn,
888 const CLSID *pPinCategoryIn,
889 BOOL bRender,
890 BOOL bOutputNeeded,
891 DWORD cOutputTypes,
892 const GUID *pOutputTypes,
893 const REGPINMEDIUM *pMedOut,
894 const CLSID *pPinCategoryOut)
896 ICreateDevEnum * pCreateDevEnum;
897 IMoniker * pMonikerCat;
898 IEnumMoniker * pEnumCat;
899 HRESULT hr;
900 struct Vector monikers = {NULL, 0, 0};
902 TRACE("(%p, %x, %s, %x, %s, %d, %p, %p, %p, %s, %s, %p, %p, %p)\n",
903 ppEnum,
904 dwFlags,
905 bExactMatch ? "true" : "false",
906 dwMerit,
907 bInputNeeded ? "true" : "false",
908 cInputTypes,
909 pInputTypes,
910 pMedIn,
911 pPinCategoryIn,
912 bRender ? "true" : "false",
913 bOutputNeeded ? "true" : "false",
914 pOutputTypes,
915 pMedOut,
916 pPinCategoryOut);
918 if (dwFlags != 0)
920 FIXME("dwFlags = %x not implemented\n", dwFlags);
923 *ppEnum = NULL;
925 hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (LPVOID*)&pCreateDevEnum);
926 if (FAILED(hr))
927 return hr;
929 hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEnumCat, 0);
930 if (FAILED(hr)) {
931 ICreateDevEnum_Release(pCreateDevEnum);
932 return hr;
935 while (IEnumMoniker_Next(pEnumCat, 1, &pMonikerCat, NULL) == S_OK)
937 IPropertyBag * pPropBagCat = NULL;
938 VARIANT var;
939 HRESULT hrSub; /* this is so that one buggy filter
940 doesn't make the whole lot fail */
942 VariantInit(&var);
944 hrSub = IMoniker_BindToStorage(pMonikerCat, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
946 if (SUCCEEDED(hrSub))
947 hrSub = IPropertyBag_Read(pPropBagCat, wszMeritName, &var, NULL);
949 if (SUCCEEDED(hrSub) && (V_UI4(&var) >= dwMerit))
951 CLSID clsidCat;
952 IEnumMoniker * pEnum;
953 IMoniker * pMoniker;
955 VariantClear(&var);
957 if (TRACE_ON(quartz))
959 VARIANT temp;
960 V_VT(&temp) = VT_EMPTY;
961 IPropertyBag_Read(pPropBagCat, wszFriendlyName, &temp, NULL);
962 TRACE("Considering category %s\n", debugstr_w(V_BSTR(&temp)));
963 VariantClear(&temp);
966 hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
968 if (SUCCEEDED(hrSub))
969 hrSub = CLSIDFromString(V_BSTR(&var), &clsidCat);
971 if (SUCCEEDED(hrSub))
972 hrSub = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
974 if (hrSub == S_OK)
976 while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
978 IPropertyBag * pPropBag = NULL;
979 VARIANT var;
980 BYTE *pData = NULL;
981 REGFILTER2 rf2;
982 DWORD i;
983 BOOL bInputMatch = !bInputNeeded;
984 BOOL bOutputMatch = !bOutputNeeded;
986 ZeroMemory(&rf2, sizeof(rf2));
987 VariantInit(&var);
989 hrSub = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBag);
991 if (TRACE_ON(quartz))
993 VARIANT temp;
994 V_VT(&temp) = VT_EMPTY;
995 IPropertyBag_Read(pPropBag, wszFriendlyName, &temp, NULL);
996 TRACE("Considering filter %s\n", debugstr_w(V_BSTR(&temp)));
997 VariantClear(&temp);
1000 if (SUCCEEDED(hrSub))
1002 hrSub = IPropertyBag_Read(pPropBag, wszFilterDataName, &var, NULL);
1005 if (SUCCEEDED(hrSub))
1006 hrSub = SafeArrayAccessData(V_ARRAY(&var), (LPVOID*)&pData);
1008 if (SUCCEEDED(hrSub))
1009 hrSub = FM2_ReadFilterData(pData, &rf2);
1011 if (pData)
1012 SafeArrayUnaccessData(V_ARRAY(&var));
1014 VariantClear(&var);
1016 /* Logic used for bInputMatch expression:
1017 * There exists some pin such that bInputNeeded implies (pin is an input and
1018 * (bRender implies pin has render flag) and major/minor types members of
1019 * pInputTypes )
1020 * bOutputMatch is similar, but without the "bRender implies ..." part
1021 * and substituting variables names containing input for output
1024 /* determine whether filter meets requirements */
1025 if (SUCCEEDED(hrSub) && (rf2.dwMerit >= dwMerit))
1027 for (i = 0; (i < rf2.u.s2.cPins2) && (!bInputMatch || !bOutputMatch); i++)
1029 const REGFILTERPINS2 * rfp2 = rf2.u.s2.rgPins2 + i;
1031 bInputMatch = bInputMatch || (!(rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
1032 (!bRender || (rfp2->dwFlags & REG_PINFLAG_B_RENDERER)) &&
1033 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cInputTypes, pInputTypes));
1034 bOutputMatch = bOutputMatch || ((rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
1035 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cOutputTypes, pOutputTypes));
1038 if (bInputMatch && bOutputMatch)
1040 struct MONIKER_MERIT mm = {pMoniker, rf2.dwMerit};
1041 IMoniker_AddRef(pMoniker);
1042 add_data(&monikers, (LPBYTE)&mm, sizeof(mm));
1046 FM2_DeleteRegFilter(&rf2);
1047 if (pPropBag)
1048 IPropertyBag_Release(pPropBag);
1049 IMoniker_Release(pMoniker);
1051 IEnumMoniker_Release(pEnum);
1055 VariantClear(&var);
1056 if (pPropBagCat)
1057 IPropertyBag_Release(pPropBagCat);
1058 IMoniker_Release(pMonikerCat);
1061 if (SUCCEEDED(hr))
1063 IMoniker ** ppMoniker;
1064 unsigned int i;
1065 ULONG nMonikerCount = monikers.current / sizeof(struct MONIKER_MERIT);
1067 /* sort the monikers in descending merit order */
1068 qsort(monikers.pData, nMonikerCount,
1069 sizeof(struct MONIKER_MERIT),
1070 mm_compare);
1072 /* construct an IEnumMoniker interface */
1073 ppMoniker = CoTaskMemAlloc(nMonikerCount * sizeof(IMoniker *));
1074 for (i = 0; i < nMonikerCount; i++)
1076 /* no need to AddRef here as already AddRef'd above */
1077 ppMoniker[i] = ((struct MONIKER_MERIT *)monikers.pData)[i].pMoniker;
1079 hr = EnumMonikerImpl_Create(ppMoniker, nMonikerCount, ppEnum);
1080 CoTaskMemFree(ppMoniker);
1083 delete_vector(&monikers);
1084 IEnumMoniker_Release(pEnumCat);
1085 ICreateDevEnum_Release(pCreateDevEnum);
1087 return hr;
1090 static HRESULT WINAPI FilterMapper3_GetICreateDevEnum(IFilterMapper3 *iface, ICreateDevEnum **ppEnum)
1092 TRACE("(%p, %p)\n", iface, ppEnum);
1093 if (!ppEnum)
1094 return E_POINTER;
1095 return CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (void**)ppEnum);
1098 static const IFilterMapper3Vtbl fm3vtbl =
1101 FilterMapper3_QueryInterface,
1102 FilterMapper3_AddRef,
1103 FilterMapper3_Release,
1105 FilterMapper3_CreateCategory,
1106 FilterMapper3_UnregisterFilter,
1107 FilterMapper3_RegisterFilter,
1108 FilterMapper3_EnumMatchingFilters,
1109 FilterMapper3_GetICreateDevEnum
1112 /*** IUnknown methods ***/
1114 static HRESULT WINAPI FilterMapper_QueryInterface(IFilterMapper * iface, REFIID riid, LPVOID *ppv)
1116 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1118 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1120 return FilterMapper3_QueryInterface(&This->IFilterMapper3_iface, riid, ppv);
1123 static ULONG WINAPI FilterMapper_AddRef(IFilterMapper * iface)
1125 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1127 return IUnknown_AddRef(This->outer_unk);
1130 static ULONG WINAPI FilterMapper_Release(IFilterMapper * iface)
1132 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1134 return IUnknown_Release(This->outer_unk);
1137 /*** IFilterMapper methods ***/
1139 static HRESULT WINAPI FilterMapper_EnumMatchingFilters(
1140 IFilterMapper * iface,
1141 IEnumRegFilters **ppEnum,
1142 DWORD dwMerit,
1143 BOOL bInputNeeded,
1144 CLSID clsInMaj,
1145 CLSID clsInSub,
1146 BOOL bRender,
1147 BOOL bOutputNeeded,
1148 CLSID clsOutMaj,
1149 CLSID clsOutSub)
1151 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1152 GUID InputType[2];
1153 GUID OutputType[2];
1154 IEnumMoniker* ppEnumMoniker;
1155 IMoniker* IMon;
1156 ULONG nb;
1157 ULONG idx = 0, nb_mon = 0;
1158 REGFILTER* regfilters;
1159 HRESULT hr;
1161 TRACE("(%p/%p)->(%p, %x, %s, %s, %s, %s, %s, %s, %s) stub!\n",
1162 iface,This,
1163 ppEnum,
1164 dwMerit,
1165 bInputNeeded ? "true" : "false",
1166 debugstr_guid(&clsInMaj),
1167 debugstr_guid(&clsInSub),
1168 bRender ? "true" : "false",
1169 bOutputNeeded ? "true" : "false",
1170 debugstr_guid(&clsOutMaj),
1171 debugstr_guid(&clsOutSub));
1173 InputType[0] = clsInMaj;
1174 InputType[1] = clsInSub;
1175 OutputType[0] = clsOutMaj;
1176 OutputType[1] = clsOutSub;
1178 *ppEnum = NULL;
1180 hr = IFilterMapper3_EnumMatchingFilters(&This->IFilterMapper3_iface, &ppEnumMoniker, 0, TRUE,
1181 dwMerit, bInputNeeded, 1, InputType, NULL, &GUID_NULL, bRender, bOutputNeeded, 1,
1182 OutputType, NULL, &GUID_NULL);
1184 if (FAILED(hr))
1185 return hr;
1187 while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1189 IMoniker_Release(IMon);
1190 nb_mon++;
1193 if (!nb_mon)
1195 IEnumMoniker_Release(ppEnumMoniker);
1196 return IEnumRegFiltersImpl_Construct(NULL, 0, ppEnum);
1199 regfilters = CoTaskMemAlloc(nb_mon * sizeof(REGFILTER));
1200 if (!regfilters)
1202 IEnumMoniker_Release(ppEnumMoniker);
1203 return E_OUTOFMEMORY;
1205 ZeroMemory(regfilters, nb_mon * sizeof(REGFILTER)); /* will prevent bad free of Name in case of error. */
1207 IEnumMoniker_Reset(ppEnumMoniker);
1208 while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1210 IPropertyBag * pPropBagCat = NULL;
1211 VARIANT var;
1212 HRESULT hrSub;
1213 GUID clsid;
1214 int len;
1216 VariantInit(&var);
1218 hrSub = IMoniker_BindToStorage(IMon, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
1220 if (SUCCEEDED(hrSub))
1221 hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
1223 if (SUCCEEDED(hrSub))
1224 hrSub = CLSIDFromString(V_BSTR(&var), &clsid);
1226 VariantClear(&var);
1228 if (SUCCEEDED(hrSub))
1229 hrSub = IPropertyBag_Read(pPropBagCat, wszFriendlyName, &var, NULL);
1231 if (SUCCEEDED(hrSub))
1233 len = (strlenW(V_BSTR(&var))+1) * sizeof(WCHAR);
1234 if (!(regfilters[idx].Name = CoTaskMemAlloc(len*2)))
1235 hr = E_OUTOFMEMORY;
1238 if (SUCCEEDED(hrSub) && regfilters[idx].Name)
1240 memcpy(regfilters[idx].Name, V_BSTR(&var), len);
1241 regfilters[idx].Clsid = clsid;
1242 idx++;
1245 if (pPropBagCat)
1246 IPropertyBag_Release(pPropBagCat);
1247 IMoniker_Release(IMon);
1248 VariantClear(&var);
1251 if (SUCCEEDED(hr))
1253 hr = IEnumRegFiltersImpl_Construct(regfilters, nb_mon, ppEnum);
1256 for (idx = 0; idx < nb_mon; idx++)
1257 CoTaskMemFree(regfilters[idx].Name);
1258 CoTaskMemFree(regfilters);
1259 IEnumMoniker_Release(ppEnumMoniker);
1261 return hr;
1265 static HRESULT WINAPI FilterMapper_RegisterFilter(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, DWORD dwMerit)
1267 HRESULT hr;
1268 LPWSTR wszClsid = NULL;
1269 HKEY hKey;
1270 LONG lRet;
1271 WCHAR wszKeyName[ARRAYSIZE(wszFilterSlash)-1 + (CHARS_IN_GUID-1) + 1];
1273 TRACE("(%p)->(%s, %s, %x)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), dwMerit);
1275 hr = StringFromCLSID(&clsid, &wszClsid);
1277 if (SUCCEEDED(hr))
1279 strcpyW(wszKeyName, wszFilterSlash);
1280 strcatW(wszKeyName, wszClsid);
1282 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1283 hr = HRESULT_FROM_WIN32(lRet);
1286 if (SUCCEEDED(hr))
1288 lRet = RegSetValueExW(hKey, NULL, 0, REG_SZ, (const BYTE*)szName, (strlenW(szName) + 1) * sizeof(WCHAR));
1289 hr = HRESULT_FROM_WIN32(lRet);
1290 RegCloseKey(hKey);
1293 if (SUCCEEDED(hr))
1295 strcpyW(wszKeyName, wszClsidSlash);
1296 strcatW(wszKeyName, wszClsid);
1298 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1299 hr = HRESULT_FROM_WIN32(lRet);
1302 if (SUCCEEDED(hr))
1304 lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwMerit, sizeof(dwMerit));
1305 hr = HRESULT_FROM_WIN32(lRet);
1306 RegCloseKey(hKey);
1309 CoTaskMemFree(wszClsid);
1311 return hr;
1314 static HRESULT WINAPI FilterMapper_RegisterFilterInstance(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, CLSID *MRId)
1316 TRACE("(%p)->(%s, %s, %p)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), MRId);
1318 /* Not implemented in Windows (tested on Win2k) */
1320 return E_NOTIMPL;
1323 static HRESULT WINAPI FilterMapper_RegisterPin(
1324 IFilterMapper * iface,
1325 CLSID Filter,
1326 LPCWSTR szName,
1327 BOOL bRendered,
1328 BOOL bOutput,
1329 BOOL bZero,
1330 BOOL bMany,
1331 CLSID ConnectsToFilter,
1332 LPCWSTR ConnectsToPin)
1334 HRESULT hr;
1335 LONG lRet;
1336 LPWSTR wszClsid = NULL;
1337 HKEY hKey = NULL;
1338 HKEY hPinsKey = NULL;
1339 WCHAR * wszPinsKeyName;
1340 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1342 TRACE("(%p)->(%s, %s, %d, %d, %d, %d, %s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(szName), bRendered,
1343 bOutput, bZero, bMany, debugstr_guid(&ConnectsToFilter), debugstr_w(ConnectsToPin));
1345 hr = StringFromCLSID(&Filter, &wszClsid);
1347 if (SUCCEEDED(hr))
1349 strcpyW(wszKeyName, wszClsidSlash);
1350 strcatW(wszKeyName, wszClsid);
1352 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1353 hr = HRESULT_FROM_WIN32(lRet);
1356 if (SUCCEEDED(hr))
1358 wszPinsKeyName = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(szName) + 1) * 2);
1359 if (!wszPinsKeyName)
1360 hr = E_OUTOFMEMORY;
1363 if (SUCCEEDED(hr))
1365 strcpyW(wszPinsKeyName, wszPins);
1366 strcatW(wszPinsKeyName, wszSlash);
1367 strcatW(wszPinsKeyName, szName);
1369 lRet = RegCreateKeyExW(hKey, wszPinsKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hPinsKey, NULL);
1370 hr = HRESULT_FROM_WIN32(lRet);
1371 CoTaskMemFree(wszPinsKeyName);
1374 if (SUCCEEDED(hr))
1376 lRet = RegSetValueExW(hPinsKey, wszAllowedMany, 0, REG_DWORD, (LPBYTE)&bMany, sizeof(bMany));
1377 hr = HRESULT_FROM_WIN32(lRet);
1380 if (SUCCEEDED(hr))
1382 lRet = RegSetValueExW(hPinsKey, wszAllowedZero, 0, REG_DWORD, (LPBYTE)&bZero, sizeof(bZero));
1383 hr = HRESULT_FROM_WIN32(lRet);
1386 if (SUCCEEDED(hr))
1388 lRet = RegSetValueExW(hPinsKey, wszDirection, 0, REG_DWORD, (LPBYTE)&bOutput, sizeof(bOutput));
1389 hr = HRESULT_FROM_WIN32(lRet);
1392 if (SUCCEEDED(hr))
1394 lRet = RegSetValueExW(hPinsKey, wszIsRendered, 0, REG_DWORD, (LPBYTE)&bRendered, sizeof(bRendered));
1395 hr = HRESULT_FROM_WIN32(lRet);
1398 if (SUCCEEDED(hr))
1400 HKEY hkeyDummy = NULL;
1402 lRet = RegCreateKeyExW(hPinsKey, wszTypes, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1403 hr = HRESULT_FROM_WIN32(lRet);
1405 if (hkeyDummy) RegCloseKey(hkeyDummy);
1408 CoTaskMemFree(wszClsid);
1409 if (hKey)
1410 RegCloseKey(hKey);
1411 if (hPinsKey)
1412 RegCloseKey(hPinsKey);
1414 return hr;
1418 static HRESULT WINAPI FilterMapper_RegisterPinType(
1419 IFilterMapper * iface,
1420 CLSID clsFilter,
1421 LPCWSTR szName,
1422 CLSID clsMajorType,
1423 CLSID clsSubType)
1425 HRESULT hr;
1426 LONG lRet;
1427 LPWSTR wszClsid = NULL;
1428 LPWSTR wszClsidMajorType = NULL;
1429 LPWSTR wszClsidSubType = NULL;
1430 HKEY hKey = NULL;
1431 WCHAR * wszTypesKey;
1432 WCHAR wszKeyName[MAX_PATH];
1434 TRACE("(%p)->(%s, %s, %s, %s)\n", iface, debugstr_guid(&clsFilter), debugstr_w(szName),
1435 debugstr_guid(&clsMajorType), debugstr_guid(&clsSubType));
1437 hr = StringFromCLSID(&clsFilter, &wszClsid);
1439 if (SUCCEEDED(hr))
1441 hr = StringFromCLSID(&clsMajorType, &wszClsidMajorType);
1444 if (SUCCEEDED(hr))
1446 hr = StringFromCLSID(&clsSubType, &wszClsidSubType);
1449 if (SUCCEEDED(hr))
1451 wszTypesKey = CoTaskMemAlloc((strlenW(wszClsidSlash) + strlenW(wszClsid) + strlenW(wszPins) +
1452 strlenW(szName) + strlenW(wszTypes) + 3 + 1) * 2);
1453 if (!wszTypesKey)
1454 hr = E_OUTOFMEMORY;
1457 if (SUCCEEDED(hr))
1459 strcpyW(wszTypesKey, wszClsidSlash);
1460 strcatW(wszTypesKey, wszClsid);
1461 strcatW(wszTypesKey, wszSlash);
1462 strcatW(wszTypesKey, wszPins);
1463 strcatW(wszTypesKey, wszSlash);
1464 strcatW(wszTypesKey, szName);
1465 strcatW(wszTypesKey, wszSlash);
1466 strcatW(wszTypesKey, wszTypes);
1468 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszTypesKey, 0, KEY_WRITE, &hKey);
1469 hr = HRESULT_FROM_WIN32(lRet);
1470 CoTaskMemFree(wszTypesKey);
1473 if (SUCCEEDED(hr))
1475 HKEY hkeyDummy = NULL;
1477 strcpyW(wszKeyName, wszClsidMajorType);
1478 strcatW(wszKeyName, wszSlash);
1479 strcatW(wszKeyName, wszClsidSubType);
1481 lRet = RegCreateKeyExW(hKey, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1482 hr = HRESULT_FROM_WIN32(lRet);
1483 RegCloseKey(hKey);
1485 if (hkeyDummy) RegCloseKey(hkeyDummy);
1488 CoTaskMemFree(wszClsid);
1489 CoTaskMemFree(wszClsidMajorType);
1490 CoTaskMemFree(wszClsidSubType);
1492 return hr;
1495 static HRESULT WINAPI FilterMapper_UnregisterFilter(IFilterMapper * iface, CLSID Filter)
1497 HRESULT hr;
1498 LONG lRet;
1499 LPWSTR wszClsid = NULL;
1500 HKEY hKey;
1501 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1503 TRACE("(%p)->(%s)\n", iface, debugstr_guid(&Filter));
1505 hr = StringFromCLSID(&Filter, &wszClsid);
1507 if (SUCCEEDED(hr))
1509 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilter, 0, KEY_WRITE, &hKey);
1510 hr = HRESULT_FROM_WIN32(lRet);
1513 if (SUCCEEDED(hr))
1515 lRet = RegDeleteKeyW(hKey, wszClsid);
1516 hr = HRESULT_FROM_WIN32(lRet);
1517 RegCloseKey(hKey);
1520 if (SUCCEEDED(hr))
1522 strcpyW(wszKeyName, wszClsidSlash);
1523 strcatW(wszKeyName, wszClsid);
1525 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1526 hr = HRESULT_FROM_WIN32(lRet);
1529 if (SUCCEEDED(hr))
1531 lRet = RegDeleteValueW(hKey, wszMeritName);
1532 if (lRet != ERROR_SUCCESS)
1533 hr = HRESULT_FROM_WIN32(lRet);
1535 lRet = RegDeleteTreeW(hKey, wszPins);
1536 if (lRet != ERROR_SUCCESS)
1537 hr = HRESULT_FROM_WIN32(lRet);
1539 RegCloseKey(hKey);
1542 CoTaskMemFree(wszClsid);
1544 return hr;
1547 static HRESULT WINAPI FilterMapper_UnregisterFilterInstance(IFilterMapper * iface, CLSID MRId)
1549 TRACE("(%p)->(%s)\n", iface, debugstr_guid(&MRId));
1551 /* Not implemented in Windows (tested on Win2k) */
1553 return E_NOTIMPL;
1556 static HRESULT WINAPI FilterMapper_UnregisterPin(IFilterMapper * iface, CLSID Filter, LPCWSTR Name)
1558 HRESULT hr;
1559 LONG lRet;
1560 LPWSTR wszClsid = NULL;
1561 HKEY hKey = NULL;
1562 WCHAR * wszPinNameKey;
1563 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1565 TRACE("(%p)->(%s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(Name));
1567 if (!Name)
1568 return E_INVALIDARG;
1570 hr = StringFromCLSID(&Filter, &wszClsid);
1572 if (SUCCEEDED(hr))
1574 strcpyW(wszKeyName, wszClsidSlash);
1575 strcatW(wszKeyName, wszClsid);
1577 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1578 hr = HRESULT_FROM_WIN32(lRet);
1581 if (SUCCEEDED(hr))
1583 wszPinNameKey = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(Name) + 1) * 2);
1584 if (!wszPinNameKey)
1585 hr = E_OUTOFMEMORY;
1588 if (SUCCEEDED(hr))
1590 strcpyW(wszPinNameKey, wszPins);
1591 strcatW(wszPinNameKey, wszSlash);
1592 strcatW(wszPinNameKey, Name);
1594 lRet = RegDeleteTreeW(hKey, wszPinNameKey);
1595 hr = HRESULT_FROM_WIN32(lRet);
1596 CoTaskMemFree(wszPinNameKey);
1599 CoTaskMemFree(wszClsid);
1600 if (hKey)
1601 RegCloseKey(hKey);
1603 return hr;
1606 static const IFilterMapperVtbl fmvtbl =
1609 FilterMapper_QueryInterface,
1610 FilterMapper_AddRef,
1611 FilterMapper_Release,
1613 FilterMapper_RegisterFilter,
1614 FilterMapper_RegisterFilterInstance,
1615 FilterMapper_RegisterPin,
1616 FilterMapper_RegisterPinType,
1617 FilterMapper_UnregisterFilter,
1618 FilterMapper_UnregisterFilterInstance,
1619 FilterMapper_UnregisterPin,
1620 FilterMapper_EnumMatchingFilters
1624 /*** IUnknown methods ***/
1625 static HRESULT WINAPI AMFilterData_QueryInterface(IAMFilterData * iface, REFIID riid, LPVOID *ppv)
1627 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1629 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
1632 static ULONG WINAPI AMFilterData_AddRef(IAMFilterData * iface)
1634 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1636 return IUnknown_AddRef(This->outer_unk);
1639 static ULONG WINAPI AMFilterData_Release(IAMFilterData * iface)
1641 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1643 return IUnknown_Release(This->outer_unk);
1646 /*** IAMFilterData methods ***/
1647 static HRESULT WINAPI AMFilterData_ParseFilterData(IAMFilterData* iface,
1648 BYTE *pData, ULONG cb,
1649 BYTE **ppRegFilter2)
1651 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1652 HRESULT hr = S_OK;
1653 static REGFILTER2 *prf2;
1655 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pData, cb, ppRegFilter2);
1657 prf2 = CoTaskMemAlloc(sizeof(*prf2));
1658 if (!prf2)
1659 return E_OUTOFMEMORY;
1660 *ppRegFilter2 = (BYTE *)&prf2;
1662 hr = FM2_ReadFilterData(pData, prf2);
1663 if (FAILED(hr))
1665 CoTaskMemFree(prf2);
1666 *ppRegFilter2 = NULL;
1669 return hr;
1672 static HRESULT WINAPI AMFilterData_CreateFilterData(IAMFilterData* iface,
1673 REGFILTER2 *prf2,
1674 BYTE **pRegFilterData,
1675 ULONG *pcb)
1677 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1679 TRACE("(%p/%p)->(%p, %p, %p)\n", This, iface, prf2, pRegFilterData, pcb);
1681 return FM2_WriteFilterData(prf2, pRegFilterData, pcb);
1684 static const IAMFilterDataVtbl AMFilterDataVtbl = {
1685 AMFilterData_QueryInterface,
1686 AMFilterData_AddRef,
1687 AMFilterData_Release,
1688 AMFilterData_ParseFilterData,
1689 AMFilterData_CreateFilterData
1692 HRESULT FilterMapper2_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1694 FilterMapper3Impl * pFM2impl;
1696 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1698 pFM2impl = CoTaskMemAlloc(sizeof(*pFM2impl));
1699 if (!pFM2impl)
1700 return E_OUTOFMEMORY;
1702 pFM2impl->IUnknown_inner.lpVtbl = &IInner_VTable;
1703 pFM2impl->IFilterMapper3_iface.lpVtbl = &fm3vtbl;
1704 pFM2impl->IFilterMapper_iface.lpVtbl = &fmvtbl;
1705 pFM2impl->IAMFilterData_iface.lpVtbl = &AMFilterDataVtbl;
1706 pFM2impl->ref = 1;
1708 if (pUnkOuter)
1709 pFM2impl->outer_unk = pUnkOuter;
1710 else
1711 pFM2impl->outer_unk = &pFM2impl->IUnknown_inner;
1713 *ppObj = &pFM2impl->IUnknown_inner;
1715 TRACE("-- created at %p\n", pFM2impl);
1717 return S_OK;
1720 HRESULT FilterMapper_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1722 FilterMapper3Impl *pFM2impl;
1723 HRESULT hr;
1725 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1727 hr = FilterMapper2_create(pUnkOuter, (LPVOID*)&pFM2impl);
1728 if (FAILED(hr))
1729 return hr;
1731 *ppObj = &pFM2impl->IFilterMapper_iface;
1733 return hr;