Document wrong behaviour for IRunningObjectTable.
[wine] / dlls / msdmo / dmoreg.c
1 /*
2  * Copyright (C) 2003 Michael Günnewig
3  * Copyright (C) 2003 CodeWeavers Inc. (Ulrich Czekalla)
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #define COM_NO_WINDOWS_H
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winerror.h"
27 #include "winreg.h"
28 #include "objbase.h"
29 #include "dmo.h"
30
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(msdmo);
35
36
37 static const WCHAR szDMORootKey[] = 
38 {
39     'D','i','r','e','c','t','S','h','o','w','\\',
40     'M','e','d','i','a','O','b','j','e','c','t','s',0
41 }; 
42
43 static const WCHAR szDMOInputType[] =
44 {
45     'I','n','p','u','t','T','y','p','e','s',0
46 };
47
48 static const WCHAR szDMOOutputType[] =
49 {
50     'O','u','t','p','u','t','T','y','p','e','s',0
51 };
52
53 static const WCHAR szDMOKeyed[] =
54 {
55     'K','e','y','e','d',0
56 };
57
58 static const WCHAR szDMOCategories[] =
59 {
60     'C','a','t','e','g','o','r','i','e','s',0
61 };
62
63 static const WCHAR szGUIDFmt[] =
64 {
65     '%','0','8','X','-','%','0','4','X','-','%','0','4','X','-','%','0',
66     '2','X','%','0','2','X','-','%','0','2','X','%','0','2','X','%','0','2',
67     'X','%','0','2','X','%','0','2','X','%','0','2','X',0
68 };
69
70 static const WCHAR szCat3Fmt[] =
71 {
72     '%','s','\\','%','s','\\','%','s',0
73 };
74
75 static const WCHAR szCat2Fmt[] =
76 {
77     '%','s','\\','%','s',0
78 };
79
80 typedef struct
81 {
82     ICOM_VFIELD(IEnumDMO);
83     DWORD                       ref;
84     DWORD                       index;
85     const GUID*                 guidCategory;
86     DWORD                       dwFlags;
87     DWORD                       cInTypes;
88     DMO_PARTIAL_MEDIATYPE       *pInTypes;
89     DWORD                       cOutTypes;
90     DMO_PARTIAL_MEDIATYPE       *pOutTypes;
91     HKEY                        hkey;
92 } IEnumDMOImpl;
93
94 const GUID IID_IEnumDMO = { 0x2c3cd98a, 0x2bfa, 0x4a53,
95     { 0x9c, 0x27, 0x52, 0x49, 0xba, 0x64, 0xba, 0x0f}};
96
97 static struct ICOM_VTABLE(IEnumDMO) edmovt;
98
99 static LPWSTR GUIDToString(LPWSTR lpwstr, REFGUID lpcguid)
100 {
101     wsprintfW(lpwstr, szGUIDFmt, lpcguid->Data1, lpcguid->Data2,
102         lpcguid->Data3, lpcguid->Data4[0], lpcguid->Data4[1],
103         lpcguid->Data4[2], lpcguid->Data4[3], lpcguid->Data4[4],
104         lpcguid->Data4[5], lpcguid->Data4[6], lpcguid->Data4[7]);
105
106     return lpwstr;
107 }
108
109 static BOOL IsMediaTypeEqual(DMO_PARTIAL_MEDIATYPE* mt1, DMO_PARTIAL_MEDIATYPE* mt2)
110 {
111
112     return (IsEqualCLSID(&mt1->type, &mt2->type) ||
113             IsEqualCLSID(&mt2->type, &GUID_NULL) ||
114             IsEqualCLSID(&mt1->type, &GUID_NULL)) &&
115             (IsEqualCLSID(&mt1->subtype, &mt2->subtype) ||
116             IsEqualCLSID(&mt2->subtype, &GUID_NULL) ||
117             IsEqualCLSID(&mt1->subtype, &GUID_NULL));
118 }
119
120 /***************************************************************
121  * DMORegister
122  *
123  * Register a DirectX Media Object.
124  */
125 HRESULT WINAPI DMORegister(
126    LPCWSTR szName,
127    REFCLSID clsidDMO,
128    REFGUID guidCategory,
129    DWORD dwFlags,
130    DWORD cInTypes,
131    const DMO_PARTIAL_MEDIATYPE *pInTypes,
132    DWORD cOutTypes,
133    const DMO_PARTIAL_MEDIATYPE *pOutTypes
134 )
135 {
136     WCHAR szguid[64];
137     HRESULT hres;
138     HKEY hrkey = 0;
139     HKEY hkey = 0;
140     HKEY hckey = 0;
141     HKEY hclskey = 0;
142
143     TRACE("%s\n", debugstr_w(szName));
144
145     hres = RegOpenKeyExW(HKEY_CLASSES_ROOT, szDMORootKey, 0, KEY_WRITE, &hrkey);
146     if (ERROR_SUCCESS != hres)
147         goto lend;
148
149     /* Create clsidDMO key under MediaObjects */ 
150     hres = RegCreateKeyExW(hrkey, GUIDToString(szguid, clsidDMO), 0, NULL,
151         REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, NULL);
152     if (ERROR_SUCCESS != hres)
153         goto lend;
154
155     /* Set default Name value */
156     hres = RegSetValueExW(hkey, NULL, 0, REG_SZ, (LPBYTE) szName, 
157         (strlenW(szName) + 1)) * sizeof(WCHAR);
158     /* Set InputTypes */
159     hres = RegSetValueExW(hkey, szDMOInputType, 0, REG_BINARY, 
160         (LPBYTE) pInTypes, cInTypes * sizeof(DMO_PARTIAL_MEDIATYPE));
161     /* Set OutputTypes */
162     hres = RegSetValueExW(hkey, szDMOOutputType, 0, REG_BINARY, 
163         (LPBYTE) pOutTypes, cOutTypes * sizeof(DMO_PARTIAL_MEDIATYPE));
164
165     if (dwFlags & DMO_REGISTERF_IS_KEYED)
166     {
167         /* Create Keyed key */ 
168         hres = RegCreateKeyExW(hkey, szDMOKeyed, 0, NULL,
169             REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hckey, NULL);
170         if (ERROR_SUCCESS != hres)
171             goto lend;
172         RegCloseKey(hckey);
173     }
174
175     /* Register the category */
176     hres = RegOpenKeyExW(hrkey, szDMOCategories, 0, KEY_WRITE, &hckey);
177     if (ERROR_SUCCESS != hres)
178         goto lend;
179
180     RegCloseKey(hkey);
181
182     hres = RegOpenKeyExW(hckey, GUIDToString(szguid, guidCategory), 0, KEY_WRITE, &hkey);
183     if (ERROR_SUCCESS != hres)
184         goto lend;
185     hres = RegCreateKeyExW(hkey, GUIDToString(szguid, clsidDMO), 0, NULL,
186         REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hclskey, NULL);
187     if (ERROR_SUCCESS != hres)
188         goto lend;
189
190 lend:
191     if (hkey)
192         RegCloseKey(hkey);
193     if (hckey)
194         RegCloseKey(hckey);
195     if (hclskey)
196         RegCloseKey(hclskey);
197     if (hrkey)
198         RegCloseKey(hrkey);
199
200     TRACE(" hresult=0x%08lx\n", hres);
201     return hres;
202 }
203
204
205 /***************************************************************
206  * DMOUnregister
207  *
208  * Unregister a DirectX Media Object.
209  */
210 HRESULT WINAPI DMOUnregister(REFCLSID clsidDMO, REFGUID guidCategory)
211 {
212     HRESULT hres;
213     WCHAR szguid[64];
214     HKEY hrkey = 0;
215     HKEY hckey = 0;
216
217     GUIDToString(szguid, clsidDMO);
218
219     TRACE("%s %p\n", debugstr_w(szguid), guidCategory);
220
221     hres = RegOpenKeyExW(HKEY_CLASSES_ROOT, szDMORootKey, 0, KEY_WRITE, &hrkey);
222     if (ERROR_SUCCESS != hres)
223         goto lend;
224
225     hres = RegDeleteKeyW(hrkey, szguid);
226     if (ERROR_SUCCESS != hres)
227         goto lend;
228
229     hres = RegOpenKeyExW(hrkey, szDMOCategories, 0, KEY_WRITE, &hckey);
230     if (ERROR_SUCCESS != hres)
231         goto lend;
232
233     hres = RegDeleteKeyW(hckey, szguid);
234     if (ERROR_SUCCESS != hres)
235         goto lend;
236
237 lend:
238     if (hckey)
239         RegCloseKey(hckey);
240     if (hrkey)
241         RegCloseKey(hrkey);
242
243     return hres;
244 }
245
246
247 /***************************************************************
248  * DMOGetName
249  *
250  * Get DMP Name from the registry
251  */
252 HRESULT WINAPI DMOGetName(REFCLSID clsidDMO, WCHAR* szName)
253 {
254     WCHAR szguid[64];
255     HRESULT hres;
256     HKEY hrkey = 0;
257     HKEY hkey = 0;
258     DWORD count;
259
260     TRACE("%s\n", debugstr_guid(clsidDMO));
261
262     hres = RegOpenKeyExW(HKEY_CLASSES_ROOT, szDMORootKey, 
263         0, KEY_READ, &hrkey);
264     if (ERROR_SUCCESS != hres)
265         goto lend;
266
267     hres = RegOpenKeyExW(hrkey, GUIDToString(szguid, clsidDMO),
268         0, KEY_READ, &hkey);
269     if (ERROR_SUCCESS != hres)
270         goto lend;
271
272     count = 80 * sizeof(WCHAR); /* 80 by API definition */
273     hres = RegQueryValueExW(hkey, NULL, NULL, NULL, 
274         (LPBYTE) szName, &count); 
275
276     TRACE(" szName=%s\n", debugstr_w(szName));
277 lend:
278     if (hkey)
279         RegCloseKey(hrkey);
280     if (hkey)
281         RegCloseKey(hkey);
282
283     return hres;
284 }
285
286
287 /**************************************************************************
288 *   IEnumDMO_Destructor
289 */
290 static BOOL IEnumDMO_Destructor(IEnumDMO* iface)
291 {
292     ICOM_THIS(IEnumDMOImpl,iface);
293
294     TRACE("%p\n", This);
295
296     if (This->hkey)
297         RegCloseKey(This->hkey);
298
299     if (This->pInTypes)
300         HeapFree(GetProcessHeap(), 0, This->pInTypes);
301
302     if (This->pOutTypes)
303         HeapFree(GetProcessHeap(), 0, This->pOutTypes);
304
305     return TRUE;
306 }
307
308
309 /**************************************************************************
310  *  IEnumDMO_Constructor
311  */
312 IEnumDMO * IEnumDMO_Constructor(
313     REFGUID guidCategory,
314     DWORD dwFlags,
315     DWORD cInTypes,
316     const DMO_PARTIAL_MEDIATYPE *pInTypes,
317     DWORD cOutTypes,
318     const DMO_PARTIAL_MEDIATYPE *pOutTypes)
319 {
320     UINT size;
321     IEnumDMOImpl* lpedmo;
322     BOOL ret = FALSE;
323
324     lpedmo = (IEnumDMOImpl*)HeapAlloc(GetProcessHeap(), 
325         HEAP_ZERO_MEMORY, sizeof(IEnumDMOImpl));
326
327     if (lpedmo)
328     {
329         lpedmo->ref = 1;
330         lpedmo->lpVtbl = &edmovt;
331         lpedmo->index = -1;
332         lpedmo->guidCategory = guidCategory;
333         lpedmo->dwFlags = dwFlags;
334
335         size = cInTypes * sizeof(DMO_PARTIAL_MEDIATYPE);
336         lpedmo->pInTypes = HeapAlloc(GetProcessHeap(), 0, size);
337         if (!lpedmo->pInTypes)
338             goto lerr;
339         memcpy(lpedmo->pInTypes, pInTypes, size);
340         lpedmo->cInTypes = cInTypes;
341
342         size = cOutTypes * sizeof(DMO_PARTIAL_MEDIATYPE);
343         lpedmo->pOutTypes = HeapAlloc(GetProcessHeap(), 0, size);
344         if (!lpedmo->pOutTypes)
345             goto lerr;
346         memcpy(lpedmo->pOutTypes, pOutTypes, size);
347         lpedmo->cOutTypes = cOutTypes;
348
349         /* If not filtering by category enum from media objects root */
350         if (IsEqualGUID(guidCategory, &GUID_NULL))
351         {
352             if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_CLASSES_ROOT, szDMORootKey, 
353                 0, KEY_READ, &lpedmo->hkey))
354                 ret = TRUE;
355         }
356         else
357         {
358             WCHAR szguid[64];
359             WCHAR szKey[MAX_PATH];
360
361             wsprintfW(szKey, szCat3Fmt, szDMORootKey, szDMOCategories, 
362                 GUIDToString(szguid, guidCategory));
363             if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_CLASSES_ROOT, szKey, 
364                 0, KEY_READ, &lpedmo->hkey))
365                 ret = TRUE;
366         }
367
368 lerr:
369         if(!ret)
370         {
371             IEnumDMO_Destructor((IEnumDMO*)lpedmo);
372             HeapFree(GetProcessHeap(),0,lpedmo);
373             lpedmo = NULL;
374         }
375     }
376
377     TRACE("returning %p\n", lpedmo);
378
379     return (IEnumDMO*)lpedmo;
380 }
381
382
383 /******************************************************************************
384  * IEnumDMO_fnAddRef
385  */
386 static ULONG WINAPI IEnumDMO_fnAddRef(IEnumDMO * iface)
387 {
388     ICOM_THIS(IEnumDMOImpl,iface);
389     return ++(This->ref);
390 }
391
392
393 /**************************************************************************
394  *  EnumDMO_QueryInterface
395  */
396 static HRESULT WINAPI IEnumDMO_fnQueryInterface(
397     IEnumDMO* iface,
398     REFIID riid,
399     LPVOID *ppvObj)
400 {
401     ICOM_THIS(IEnumDMOImpl,iface);
402
403     *ppvObj = NULL;
404
405     if(IsEqualIID(riid, &IID_IUnknown))
406         *ppvObj = This;
407     else if(IsEqualIID(riid, &IID_IEnumDMO))
408         *ppvObj = (IEnumDMO*)This;
409
410     if(*ppvObj)
411     {
412         IEnumDMO_fnAddRef((IEnumDMO*)*ppvObj);
413         return S_OK;
414     }
415
416     return E_NOINTERFACE;
417 }
418
419
420 /******************************************************************************
421  * IEnumDMO_fnRelease
422  */
423 static ULONG WINAPI IEnumDMO_fnRelease(IEnumDMO * iface)
424 {
425     ICOM_THIS(IEnumDMOImpl,iface);
426
427     if (!--(This->ref))
428     {
429         IEnumDMO_Destructor((IEnumDMO*)This);
430         HeapFree(GetProcessHeap(),0,This);
431         return 0;
432     }
433
434     return This->ref;
435 }
436
437
438 /******************************************************************************
439  * IEnumDMO_fnNext
440  */
441 static HRESULT WINAPI IEnumDMO_fnNext(
442     IEnumDMO * iface, 
443     DWORD cItemsToFetch,
444     CLSID * pCLSID,
445     WCHAR ** Names,
446     DWORD * pcItemsFetched)
447 {
448     FILETIME ft;
449     HKEY hkey;
450     WCHAR szNextKey[MAX_PATH];
451     WCHAR szKey[MAX_PATH];
452     WCHAR szValue[MAX_PATH];
453     DWORD len;
454     UINT count = 0;
455     HRESULT hres = S_OK;
456
457     ICOM_THIS(IEnumDMOImpl,iface);
458
459     TRACE("%ld\n", cItemsToFetch);
460
461     if (!pCLSID || !Names || !pcItemsFetched)
462         return E_POINTER;
463
464     while (count < (INT)cItemsToFetch)
465     {
466         This->index++;
467
468         hres = RegEnumKeyExW(This->hkey, This->index, szNextKey, &len, NULL, NULL, NULL, &ft);
469         if (hres != ERROR_SUCCESS)
470             break;
471
472         TRACE("found %s\n", debugstr_w(szNextKey));
473
474         if (This->dwFlags & DMO_REGISTERF_IS_KEYED)
475         {
476             wsprintfW(szKey, szCat3Fmt, szDMORootKey, szNextKey, szDMOKeyed);
477             hres = RegOpenKeyExW(HKEY_CLASSES_ROOT, szKey, 0, KEY_READ, &hkey);
478             if (ERROR_SUCCESS != hres)
479                 continue;
480             RegCloseKey(hkey);
481         }
482
483         wsprintfW(szKey, szCat2Fmt, szDMORootKey, szNextKey);
484         hres = RegOpenKeyExW(HKEY_CLASSES_ROOT, szKey, 0, KEY_READ, &hkey);
485
486         if (This->pInTypes)
487         {
488             INT i, j;
489             DWORD cInTypes;
490             DMO_PARTIAL_MEDIATYPE* pInTypes;
491
492             len = MAX_PATH * sizeof(WCHAR);
493             hres = RegQueryValueExW(hkey, szDMOInputType, NULL, NULL, (LPBYTE) szValue, &len);
494             if (ERROR_SUCCESS != hres)
495             {
496                 RegCloseKey(hkey);
497                 continue;
498             }
499
500             cInTypes = len / sizeof(DMO_PARTIAL_MEDIATYPE);
501             pInTypes = (DMO_PARTIAL_MEDIATYPE*) szValue;
502
503             for (i = 0; i < This->cInTypes; i++)
504             {
505                 for (j = 0; j < cInTypes; j++) 
506                 {
507                     if (IsMediaTypeEqual(&pInTypes[j], &This->pInTypes[i]))
508                         break;
509                 }
510
511                 if (j >= cInTypes)
512                     break;
513             }
514
515             if (i < This->cInTypes)
516             {
517                 RegCloseKey(hkey);
518                 continue;
519             }
520         }
521
522         if (This->pOutTypes)
523         {
524             INT i, j;
525             DWORD cOutTypes;
526             DMO_PARTIAL_MEDIATYPE* pOutTypes;
527
528             len = MAX_PATH * sizeof(WCHAR);
529             hres = RegQueryValueExW(hkey, szDMOOutputType, NULL, NULL, (LPBYTE) szValue, &len);
530             if (ERROR_SUCCESS != hres)
531             {
532                 RegCloseKey(hkey);
533                 continue;
534             }
535
536             cOutTypes = len / sizeof(DMO_PARTIAL_MEDIATYPE);
537             pOutTypes = (DMO_PARTIAL_MEDIATYPE*) szValue;
538
539             for (i = 0; i < This->cOutTypes; i++)
540             {
541                 for (j = 0; j < cOutTypes; j++) 
542                 {
543                     if (IsMediaTypeEqual(&pOutTypes[j], &This->pOutTypes[i]))
544                         break;
545                 }
546
547                 if (j >= cOutTypes)
548                     break;
549             }
550
551             if (i < This->cOutTypes)
552             {
553                 RegCloseKey(hkey);
554                 continue;
555             }
556         }
557
558         /* Media object wasn't filtered so add it to return list */
559         Names[count] = NULL;
560         len = MAX_PATH * sizeof(WCHAR);
561         hres = RegQueryValueExW(hkey, NULL, NULL, NULL, (LPBYTE) szValue, &len); 
562         if (ERROR_SUCCESS == hres)
563         {
564             Names[count] = HeapAlloc(GetProcessHeap(), 0, strlenW(szValue) + 1);
565             if (Names[count])
566                 strcmpW(Names[count], szValue);
567         }
568         CLSIDFromString(szNextKey, &pCLSID[count]);
569
570         TRACE("found match %s %s\n", debugstr_w(szValue), debugstr_w(szNextKey));
571         RegCloseKey(hkey);
572         count++;
573     }
574
575     *pcItemsFetched = count;
576     if (*pcItemsFetched < cItemsToFetch)
577         hres = S_FALSE;
578
579     return hres;
580 }
581  
582
583 /******************************************************************************
584  * IEnumDMO_fnSkip
585  */
586 static HRESULT WINAPI IEnumDMO_fnSkip(IEnumDMO * iface, DWORD cItemsToSkip)
587 {
588     ICOM_THIS(IEnumDMOImpl,iface);
589
590     This->index += cItemsToSkip;
591
592     return S_OK;
593 }
594
595
596 /******************************************************************************
597  * IEnumDMO_fnReset
598  */
599 static HRESULT WINAPI IEnumDMO_fnReset(IEnumDMO * iface)
600 {
601     ICOM_THIS(IEnumDMOImpl,iface);
602
603     This->index = -1;
604
605     return S_OK;
606 }
607  
608
609 /******************************************************************************
610  * IEnumDMO_fnClone
611  */
612 static HRESULT WINAPI IEnumDMO_fnClone(IEnumDMO * iface, IEnumDMO **ppEnum)
613 {
614     ICOM_THIS(IEnumDMOImpl,iface);
615
616     FIXME("(%p)->() to (%p)->() E_NOTIMPL\n", This, ppEnum);
617
618   return E_NOTIMPL;
619 }
620
621
622 /***************************************************************
623  * DMOEnum
624  *
625  * Enumerate DirectX Media Objects in the registry.
626  */
627 HRESULT WINAPI DMOEnum(
628     REFGUID guidCategory,
629     DWORD dwFlags,
630     DWORD cInTypes,
631     const DMO_PARTIAL_MEDIATYPE *pInTypes,
632     DWORD cOutTypes,
633     const DMO_PARTIAL_MEDIATYPE *pOutTypes,
634     IEnumDMO **ppEnum)
635 {
636     HRESULT hres = E_FAIL;
637
638     TRACE("guidCategory=%p dwFlags=0x%08lx cInTypes=%ld cOutTypes=%ld\n",
639         guidCategory, dwFlags, cInTypes, cOutTypes);
640
641     *ppEnum = IEnumDMO_Constructor(guidCategory, dwFlags, cInTypes,
642         pInTypes, cOutTypes, pOutTypes);
643     if (*ppEnum)
644         hres = S_OK;
645
646     return hres;
647 }
648
649
650 static ICOM_VTABLE (IEnumDMO) edmovt =
651 {
652         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
653         IEnumDMO_fnQueryInterface,
654         IEnumDMO_fnAddRef,
655         IEnumDMO_fnRelease,
656         IEnumDMO_fnNext,
657         IEnumDMO_fnSkip,
658         IEnumDMO_fnReset,
659         IEnumDMO_fnClone,
660 };
661
662
663 HRESULT WINAPI DMOGetTypes(REFCLSID a, unsigned long b, unsigned long* c,
664                            DMO_PARTIAL_MEDIATYPE* d, unsigned long e,
665                            unsigned long* f, DMO_PARTIAL_MEDIATYPE* g)
666 {
667   FIXME("(%p,%lu,%p,%p,%lu,%p,%p),stub!\n",a,b,c,d,e,f,g);
668
669   return E_NOTIMPL;
670 }