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