createdevenum.c 27.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *	ICreateDevEnum implementation for DEVENUM.dll
 *
 * Copyright (C) 2002 Robert Shearman
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25
 *
 * NOTES ON THIS FILE:
 * - Implements ICreateDevEnum interface which creates an IEnumMoniker
 *   implementation
 * - Also creates the special registry keys created at run-time
 */

26 27 28
#define NONAMELESSSTRUCT
#define NONAMELESSUNION

29
#include "devenum_private.h"
30
#include "vfw.h"
31
#include "aviriff.h"
32
#include "dsound.h"
33 34

#include "wine/debug.h"
35
#include "wine/unicode.h"
36
#include "wine/heap.h"
37 38
#include "mmddk.h"

39
#include "initguid.h"
40
#include "wine/fil_data.h"
41

42 43
WINE_DEFAULT_DEBUG_CHANNEL(devenum);

44 45 46 47 48 49 50 51
static const WCHAR wszFilterKeyName[] = {'F','i','l','t','e','r',0};
static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
static const WCHAR wszPins[] = {'P','i','n','s',0};
static const WCHAR wszAllowedMany[] = {'A','l','l','o','w','e','d','M','a','n','y',0};
static const WCHAR wszAllowedZero[] = {'A','l','l','o','w','e','d','Z','e','r','o',0};
static const WCHAR wszDirection[] = {'D','i','r','e','c','t','i','o','n',0};
static const WCHAR wszIsRendered[] = {'I','s','R','e','n','d','e','r','e','d',0};
static const WCHAR wszTypes[] = {'T','y','p','e','s',0};
52
static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
53
static const WCHAR wszFilterData[] = {'F','i','l','t','e','r','D','a','t','a',0};
54 55

static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface);
56
static HRESULT DEVENUM_CreateAMCategoryKey(const CLSID * clsidCategory);
57 58 59 60

/**********************************************************************
 * DEVENUM_ICreateDevEnum_QueryInterface (also IUnknown)
 */
61 62
static HRESULT WINAPI DEVENUM_ICreateDevEnum_QueryInterface(ICreateDevEnum *iface, REFIID riid,
        void **ppv)
63
{
64
    TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
65

66 67
    if (!ppv)
        return E_POINTER;
68 69 70 71

    if (IsEqualGUID(riid, &IID_IUnknown) ||
	IsEqualGUID(riid, &IID_ICreateDevEnum))
    {
72
        *ppv = iface;
73 74 75 76
	DEVENUM_ICreateDevEnum_AddRef(iface);
	return S_OK;
    }

77
    FIXME("- no interface IID: %s\n", debugstr_guid(riid));
78
    *ppv = NULL;
79 80 81 82 83 84 85 86 87 88
    return E_NOINTERFACE;
}

/**********************************************************************
 * DEVENUM_ICreateDevEnum_AddRef (also IUnknown)
 */
static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface)
{
    TRACE("\n");

89
    DEVENUM_LockModule();
90

91
    return 2; /* non-heap based object */
92 93 94 95 96 97 98 99 100
}

/**********************************************************************
 * DEVENUM_ICreateDevEnum_Release (also IUnknown)
 */
static ULONG WINAPI DEVENUM_ICreateDevEnum_Release(ICreateDevEnum * iface)
{
    TRACE("\n");

101
    DEVENUM_UnlockModule();
102

103
    return 1; /* non-heap based object */
104 105
}

106 107
static HRESULT register_codec(const GUID *class, const WCHAR *name,
        const GUID *clsid, const WCHAR *friendly_name, IPropertyBag **ret)
108 109
{
    static const WCHAR deviceW[] = {'@','d','e','v','i','c','e',':','c','m',':',0};
110
    WCHAR guidstr[CHARS_IN_GUID];
111
    IParseDisplayName *parser;
112 113
    IPropertyBag *propbag;
    IMoniker *mon;
114
    WCHAR *buffer;
115
    VARIANT var;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    ULONG eaten;
    HRESULT hr;

    hr = CoCreateInstance(&CLSID_CDeviceMoniker, NULL, CLSCTX_INPROC, &IID_IParseDisplayName, (void **)&parser);
    if (FAILED(hr))
        return hr;

    buffer = heap_alloc((strlenW(deviceW) + CHARS_IN_GUID + strlenW(name) + 1) * sizeof(WCHAR));
    if (!buffer)
    {
        IParseDisplayName_Release(parser);
        return E_OUTOFMEMORY;
    }

    strcpyW(buffer, deviceW);
    StringFromGUID2(class, buffer + strlenW(buffer), CHARS_IN_GUID);
    strcatW(buffer, backslashW);
    strcatW(buffer, name);

135
    IParseDisplayName_ParseDisplayName(parser, NULL, buffer, &eaten, &mon);
136 137
    IParseDisplayName_Release(parser);
    heap_free(buffer);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

    IMoniker_BindToStorage(mon, NULL, NULL, &IID_IPropertyBag, (void **)&propbag);
    IMoniker_Release(mon);

    V_VT(&var) = VT_BSTR;
    V_BSTR(&var) = SysAllocString(friendly_name);
    hr = IPropertyBag_Write(propbag, wszFriendlyName, &var);
    VariantClear(&var);
    if (FAILED(hr))
    {
        IPropertyBag_Release(propbag);
        return hr;
    }

    V_VT(&var) = VT_BSTR;
    StringFromGUID2(clsid, guidstr, ARRAY_SIZE(guidstr));
    V_BSTR(&var) = SysAllocString(guidstr);
    hr = IPropertyBag_Write(propbag, clsidW, &var);
    VariantClear(&var);
    if (FAILED(hr))
    {
        IPropertyBag_Release(propbag);
        return hr;
    }

    *ret = propbag;
    return S_OK;
165 166
}

167
static void DEVENUM_ReadPinTypes(HKEY hkeyPinKey, REGFILTERPINS2 *rgPin)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
{
    HKEY hkeyTypes = NULL;
    DWORD dwMajorTypes, i;
    REGPINTYPES *lpMediaType = NULL;
    DWORD dwMediaTypeSize = 0;

    if (RegOpenKeyExW(hkeyPinKey, wszTypes, 0, KEY_READ, &hkeyTypes) != ERROR_SUCCESS)
        return ;

    if (RegQueryInfoKeyW(hkeyTypes, NULL, NULL, NULL, &dwMajorTypes, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
                != ERROR_SUCCESS)
    {
        RegCloseKey(hkeyTypes);
        return ;
    }

    for (i = 0; i < dwMajorTypes; i++)
    {
        HKEY hkeyMajorType = NULL;
        WCHAR wszMajorTypeName[64];
188
        DWORD cName = ARRAY_SIZE(wszMajorTypeName);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        DWORD dwMinorTypes, i1;

        if (RegEnumKeyExW(hkeyTypes, i, wszMajorTypeName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;

        if (RegOpenKeyExW(hkeyTypes, wszMajorTypeName, 0, KEY_READ, &hkeyMajorType) != ERROR_SUCCESS) continue;

        if (RegQueryInfoKeyW(hkeyMajorType, NULL, NULL, NULL, &dwMinorTypes, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
                    != ERROR_SUCCESS)
        {
            RegCloseKey(hkeyMajorType);
            continue;
        }

        for (i1 = 0; i1 < dwMinorTypes; i1++)
        {
            WCHAR wszMinorTypeName[64];
            CLSID *clsMajorType = NULL, *clsMinorType = NULL;
            HRESULT hr;

208
            cName = ARRAY_SIZE(wszMinorTypeName);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
            if (RegEnumKeyExW(hkeyMajorType, i1, wszMinorTypeName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;

            clsMinorType = CoTaskMemAlloc(sizeof(CLSID));
            if (!clsMinorType) continue;

            clsMajorType = CoTaskMemAlloc(sizeof(CLSID));
            if (!clsMajorType) goto error_cleanup_types;

            hr = CLSIDFromString(wszMinorTypeName, clsMinorType);
            if (FAILED(hr)) goto error_cleanup_types;

            hr = CLSIDFromString(wszMajorTypeName, clsMajorType);
            if (FAILED(hr)) goto error_cleanup_types;

            if (rgPin->nMediaTypes == dwMediaTypeSize)
            {
225
                DWORD dwNewSize = dwMediaTypeSize + (dwMediaTypeSize < 2 ? 1 : dwMediaTypeSize / 2);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
                REGPINTYPES *lpNewMediaType;

                lpNewMediaType = CoTaskMemRealloc(lpMediaType, sizeof(REGPINTYPES) * dwNewSize);
                if (!lpNewMediaType) goto error_cleanup_types;

                lpMediaType = lpNewMediaType;
                dwMediaTypeSize = dwNewSize;
             }

            lpMediaType[rgPin->nMediaTypes].clsMajorType = clsMajorType;
            lpMediaType[rgPin->nMediaTypes].clsMinorType = clsMinorType;
            rgPin->nMediaTypes++;
            continue;

            error_cleanup_types:

242 243
            CoTaskMemFree(clsMajorType);
            CoTaskMemFree(clsMinorType);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        }

        RegCloseKey(hkeyMajorType);
    }

    RegCloseKey(hkeyTypes);

    if (lpMediaType && !rgPin->nMediaTypes)
    {
        CoTaskMemFree(lpMediaType);
        lpMediaType = NULL;
    }

    rgPin->lpMediaType = lpMediaType;
}

static void DEVENUM_ReadPins(HKEY hkeyFilterClass, REGFILTER2 *rgf2)
{
    HKEY hkeyPins = NULL;
    DWORD dwPinsSubkeys, i;
264 265 266 267 268
    REGFILTERPINS2 *rgPins = NULL;

    rgf2->dwVersion = 2;
    rgf2->u.s2.cPins2 = 0;
    rgf2->u.s2.rgPins2 = NULL;
269 270 271 272 273 274 275 276 277 278 279 280 281

    if (RegOpenKeyExW(hkeyFilterClass, wszPins, 0, KEY_READ, &hkeyPins) != ERROR_SUCCESS)
        return ;

    if (RegQueryInfoKeyW(hkeyPins, NULL, NULL, NULL, &dwPinsSubkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
                != ERROR_SUCCESS)
    {
        RegCloseKey(hkeyPins);
        return ;
    }

    if (dwPinsSubkeys)
    {
282
        rgPins = CoTaskMemAlloc(sizeof(REGFILTERPINS2) * dwPinsSubkeys);
283 284 285 286 287 288 289 290 291 292 293
        if (!rgPins)
        {
            RegCloseKey(hkeyPins);
            return ;
        }
    }

    for (i = 0; i < dwPinsSubkeys; i++)
    {
        HKEY hkeyPinKey = NULL;
        WCHAR wszPinName[MAX_PATH];
294
        DWORD cName = ARRAY_SIZE(wszPinName);
295 296
        REGFILTERPINS2 *rgPin = &rgPins[rgf2->u.s2.cPins2];
        DWORD value, size, Type;
297 298
        LONG lRet;

299
        memset(rgPin, 0, sizeof(*rgPin));
300 301 302 303 304

        if (RegEnumKeyExW(hkeyPins, i, wszPinName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;

        if (RegOpenKeyExW(hkeyPins, wszPinName, 0, KEY_READ, &hkeyPinKey) != ERROR_SUCCESS) continue;

305 306
        size = sizeof(DWORD);
        lRet = RegQueryValueExW(hkeyPinKey, wszAllowedMany, NULL, &Type, (BYTE *)&value, &size);
307 308
        if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
            goto error_cleanup;
309 310
        if (value)
            rgPin->dwFlags |= REG_PINFLAG_B_MANY;
311

312 313
        size = sizeof(DWORD);
        lRet = RegQueryValueExW(hkeyPinKey, wszAllowedZero, NULL, &Type, (BYTE *)&value, &size);
314 315
        if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
            goto error_cleanup;
316 317
        if (value)
            rgPin->dwFlags |= REG_PINFLAG_B_ZERO;
318

319 320
        size = sizeof(DWORD);
        lRet = RegQueryValueExW(hkeyPinKey, wszDirection, NULL, &Type, (BYTE *)&value, &size);
321 322
        if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
            goto error_cleanup;
323 324
        if (value)
            rgPin->dwFlags |= REG_PINFLAG_B_OUTPUT;
325

326 327 328

        size = sizeof(DWORD);
        lRet = RegQueryValueExW(hkeyPinKey, wszIsRendered, NULL, &Type, (BYTE *)&value, &size);
329 330
        if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
            goto error_cleanup;
331 332
        if (value)
            rgPin->dwFlags |= REG_PINFLAG_B_RENDERER;
333 334 335

        DEVENUM_ReadPinTypes(hkeyPinKey, rgPin);

336
        ++rgf2->u.s2.cPins2;
337 338 339 340 341 342 343 344 345
        continue;

        error_cleanup:

        RegCloseKey(hkeyPinKey);
    }

    RegCloseKey(hkeyPins);

346
    if (rgPins && !rgf2->u.s2.cPins2)
347 348 349 350 351
    {
        CoTaskMemFree(rgPins);
        rgPins = NULL;
    }

352
    rgf2->u.s2.rgPins2 = rgPins;
353 354
}

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
static void free_regfilter2(REGFILTER2 *rgf)
{
    if (rgf->u.s2.rgPins2)
    {
        UINT iPin;

        for (iPin = 0; iPin < rgf->u.s2.cPins2; iPin++)
        {
            if (rgf->u.s2.rgPins2[iPin].lpMediaType)
            {
                UINT iType;

                for (iType = 0; iType < rgf->u.s2.rgPins2[iPin].nMediaTypes; iType++)
                {
                    CoTaskMemFree((void *)rgf->u.s2.rgPins2[iPin].lpMediaType[iType].clsMajorType);
                    CoTaskMemFree((void *)rgf->u.s2.rgPins2[iPin].lpMediaType[iType].clsMinorType);
                }

                CoTaskMemFree((void *)rgf->u.s2.rgPins2[iPin].lpMediaType);
            }
        }

        CoTaskMemFree((void *)rgf->u.s2.rgPins2);
    }
}

static void write_filter_data(IPropertyBag *prop_bag, REGFILTER2 *rgf)
{
    IAMFilterData *fildata;
    SAFEARRAYBOUND sabound;
    BYTE *data, *array;
    VARIANT var = {};
    ULONG size;
    HRESULT hr;

    hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC, &IID_IAMFilterData, (void **)&fildata);
    if (FAILED(hr)) goto cleanup;

    hr = IAMFilterData_CreateFilterData(fildata, rgf, &data, &size);
    if (FAILED(hr)) goto cleanup;

    V_VT(&var) = VT_ARRAY | VT_UI1;
    sabound.lLbound = 0;
    sabound.cElements = size;
    if (!(V_ARRAY(&var) = SafeArrayCreate(VT_UI1, 1, &sabound)))
        goto cleanup;
    hr = SafeArrayAccessData(V_ARRAY(&var), (void *)&array);
    if (FAILED(hr)) goto cleanup;

    memcpy(array, data, size);
    hr = SafeArrayUnaccessData(V_ARRAY(&var));
    if (FAILED(hr)) goto cleanup;

    hr = IPropertyBag_Write(prop_bag, wszFilterData, &var);
    if (FAILED(hr)) goto cleanup;

cleanup:
    VariantClear(&var);
    CoTaskMemFree(data);
    IAMFilterData_Release(fildata);
}

static void register_legacy_filters(void)
418 419 420 421 422 423
{
    HKEY hkeyFilter = NULL;
    DWORD dwFilterSubkeys, i;
    LONG lRet;
    HRESULT hr;

424 425
    lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilterKeyName, 0, KEY_READ, &hkeyFilter);
    hr = HRESULT_FROM_WIN32(lRet);
426 427 428 429 430 431 432

    if (SUCCEEDED(hr))
    {
        lRet = RegQueryInfoKeyW(hkeyFilter, NULL, NULL, NULL, &dwFilterSubkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
        hr = HRESULT_FROM_WIN32(lRet);
    }

433 434 435
    if (SUCCEEDED(hr))
        hr = DEVENUM_CreateAMCategoryKey(&CLSID_LegacyAmFilterCategory);

436 437 438 439 440
    if (SUCCEEDED(hr))
    {
        for (i = 0; i < dwFilterSubkeys; i++)
        {
            WCHAR wszFilterSubkeyName[64];
441
            DWORD cName = ARRAY_SIZE(wszFilterSubkeyName);
442
            IPropertyBag *prop_bag = NULL;
443
            WCHAR wszRegKey[MAX_PATH];
444 445 446
            HKEY classkey = NULL;
            REGFILTER2 rgf2;
            DWORD Type, len;
447
            GUID clsid;
448 449 450

            if (RegEnumKeyExW(hkeyFilter, i, wszFilterSubkeyName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;

451
            TRACE("Registering %s\n", debugstr_w(wszFilterSubkeyName));
452

453 454 455 456
            hr = CLSIDFromString(wszFilterSubkeyName, &clsid);
            if (FAILED(hr))
                continue;

457
            strcpyW(wszRegKey, clsidW);
458
            strcatW(wszRegKey, backslashW);
459 460
            strcatW(wszRegKey, wszFilterSubkeyName);

461 462
            if (RegOpenKeyExW(HKEY_CLASSES_ROOT, wszRegKey, 0, KEY_READ, &classkey) != ERROR_SUCCESS)
                continue;
463

464 465 466 467 468
            len = 0;
            if (!RegQueryValueExW(classkey, NULL, NULL, &Type, NULL, &len))
            {
                WCHAR *friendlyname = heap_alloc(len);
                if (!friendlyname)
469 470 471 472
                {
                    RegCloseKey(classkey);
                    continue;
                }
473
                RegQueryValueExW(classkey, NULL, NULL, &Type, (BYTE *)friendlyname, &len);
474 475 476 477

                hr = register_codec(&CLSID_LegacyAmFilterCategory, wszFilterSubkeyName,
                        &clsid, friendlyname, &prop_bag);

478
                heap_free(friendlyname);
479
            }
480
            else
481 482 483 484 485 486 487
                hr = register_codec(&CLSID_LegacyAmFilterCategory, wszFilterSubkeyName,
                        &clsid, wszFilterSubkeyName, &prop_bag);
            if (FAILED(hr))
            {
                RegCloseKey(classkey);
                continue;
            }
488 489 490 491 492 493 494 495 496 497 498

            /* write filter data */
            rgf2.dwMerit = MERIT_NORMAL;

            len = sizeof(rgf2.dwMerit);
            RegQueryValueExW(classkey, wszMeritName, NULL, &Type, (BYTE *)&rgf2.dwMerit, &len);

            DEVENUM_ReadPins(classkey, &rgf2);

            write_filter_data(prop_bag, &rgf2);

499
            IPropertyBag_Release(prop_bag);
500 501
            RegCloseKey(classkey);
            free_regfilter2(&rgf2);
502 503 504 505 506 507
        }
    }

    if (hkeyFilter) RegCloseKey(hkeyFilter);
}

508 509 510 511 512 513 514 515 516 517 518 519 520 521
static BOOL CALLBACK register_dsound_devices(GUID *guid, const WCHAR *desc, const WCHAR *module, void *context)
{
    static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',' ','D','i','r','e','c','t','S','o','u','n','d',' ','D','e','v','i','c','e',0};
    static const WCHAR directsoundW[] = {'D','i','r','e','c','t','S','o','u','n','d',':',' ',0};
    static const WCHAR dsguidW[] = {'D','S','G','u','i','d',0};
    IPropertyBag *prop_bag = NULL;
    REGFILTERPINS2 rgpins = {0};
    REGPINTYPES rgtypes = {0};
    REGFILTER2 rgf = {0};
    WCHAR clsid[CHARS_IN_GUID];
    VARIANT var;
    HRESULT hr;

    hr = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
522 523
    if (FAILED(hr))
        return FALSE;
524 525 526 527 528

    if (guid)
    {
        WCHAR *name = heap_alloc(sizeof(defaultW) + strlenW(desc) * sizeof(WCHAR));
        if (!name)
529
            return FALSE;
530 531 532
        strcpyW(name, directsoundW);
        strcatW(name, desc);

533 534
        hr = register_codec(&CLSID_AudioRendererCategory, name,
                &CLSID_DSoundRender, name, &prop_bag);
535 536 537
        heap_free(name);
    }
    else
538 539 540 541
        hr = register_codec(&CLSID_AudioRendererCategory, defaultW,
                &CLSID_DSoundRender, defaultW, &prop_bag);
    if (FAILED(hr))
        return FALSE;
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

    /* write filter data */
    rgf.dwVersion = 2;
    rgf.dwMerit = guid ? MERIT_DO_NOT_USE : MERIT_PREFERRED;
    rgf.u.s2.cPins2 = 1;
    rgf.u.s2.rgPins2 = &rgpins;
    rgpins.dwFlags = REG_PINFLAG_B_RENDERER;
    /* FIXME: native registers many more formats */
    rgpins.nMediaTypes = 1;
    rgpins.lpMediaType = &rgtypes;
    rgtypes.clsMajorType = &MEDIATYPE_Audio;
    rgtypes.clsMinorType = &MEDIASUBTYPE_PCM;

    write_filter_data(prop_bag, &rgf);

    /* write DSound guid */
    V_VT(&var) = VT_BSTR;
    StringFromGUID2(guid ? guid : &GUID_NULL, clsid, CHARS_IN_GUID);
560 561
    if ((V_BSTR(&var) = SysAllocString(clsid)))
        hr = IPropertyBag_Write(prop_bag, dsguidW, &var);
562 563

    VariantClear(&var);
564
    IPropertyBag_Release(prop_bag);
565 566 567
    return TRUE;
}

568 569 570 571 572 573 574 575 576
static void register_waveout_devices(void)
{
    static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',' ','W','a','v','e','O','u','t',' ','D','e','v','i','c','e',0};
    static const WCHAR waveoutidW[] = {'W','a','v','e','O','u','t','I','d',0};
    IPropertyBag *prop_bag = NULL;
    REGFILTERPINS2 rgpins = {0};
    REGPINTYPES rgtypes = {0};
    REGFILTER2 rgf = {0};
    WAVEOUTCAPSW caps;
577
    const WCHAR *name;
578 579 580 581 582 583 584 585 586 587 588 589 590
    int i, count;
    VARIANT var;
    HRESULT hr;

    hr = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
    if (FAILED(hr)) return;

    count = waveOutGetNumDevs();

    for (i = -1; i < count; i++)
    {
        waveOutGetDevCapsW(i, &caps, sizeof(caps));

591
        name = (i == -1) ? defaultW : caps.szPname;
592

593 594 595 596
        hr = register_codec(&CLSID_AudioRendererCategory, name,
                &CLSID_AudioRender, name, &prop_bag);
        if (FAILED(hr))
            continue;
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

        /* write filter data */
        rgf.dwVersion = 2;
        rgf.dwMerit = MERIT_DO_NOT_USE;
        rgf.u.s2.cPins2 = 1;
        rgf.u.s2.rgPins2 = &rgpins;
        rgpins.dwFlags = REG_PINFLAG_B_RENDERER;
        rgpins.nMediaTypes = 1;
        rgpins.lpMediaType = &rgtypes;
        rgtypes.clsMajorType = &MEDIATYPE_Audio;
        rgtypes.clsMinorType = &MEDIASUBTYPE_NULL;

        write_filter_data(prop_bag, &rgf);

        /* write WaveOutId */
        V_VT(&var) = VT_I4;
        V_I4(&var) = i;
614
        IPropertyBag_Write(prop_bag, waveoutidW, &var);
615 616 617 618 619 620

        VariantClear(&var);
        if (prop_bag) IPropertyBag_Release(prop_bag);
    }
}

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
static void register_wavein_devices(void)
{
    static const WCHAR waveinidW[] = {'W','a','v','e','I','n','I','d',0};
    IPropertyBag *prop_bag = NULL;
    REGFILTER2 rgf = {0};
    WAVEINCAPSW caps;
    int i, count;
    VARIANT var;
    HRESULT hr;

    hr = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
    if (FAILED(hr)) return;

    count = waveInGetNumDevs();

    for (i = 0; i < count; i++)
    {
        waveInGetDevCapsW(i, &caps, sizeof(caps));

640 641 642 643
        hr = register_codec(&CLSID_AudioInputDeviceCategory, caps.szPname,
                &CLSID_AudioRecord, caps.szPname, &prop_bag);
        if (FAILED(hr))
            continue;
644 645 646 647 648 649 650 651 652 653

        /* write filter data */
        rgf.dwVersion = 2;
        rgf.dwMerit = MERIT_DO_NOT_USE;

        write_filter_data(prop_bag, &rgf);

        /* write WaveInId */
        V_VT(&var) = VT_I4;
        V_I4(&var) = i;
654
        IPropertyBag_Write(prop_bag, waveinidW, &var);
655 656

        VariantClear(&var);
657
        IPropertyBag_Release(prop_bag);
658 659 660
    }
}

661 662 663 664 665 666 667 668 669
static void register_midiout_devices(void)
{
    static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',' ','M','i','d','i','O','u','t',' ','D','e','v','i','c','e',0};
    static const WCHAR midioutidW[] = {'M','i','d','i','O','u','t','I','d',0};
    IPropertyBag *prop_bag = NULL;
    REGFILTERPINS2 rgpins = {0};
    REGPINTYPES rgtypes = {0};
    REGFILTER2 rgf = {0};
    MIDIOUTCAPSW caps;
670
    const WCHAR *name;
671 672 673 674 675 676 677 678 679 680 681 682 683
    int i, count;
    VARIANT var;
    HRESULT hr;

    hr = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
    if (FAILED(hr)) return;

    count = midiOutGetNumDevs();

    for (i = -1; i < count; i++)
    {
        midiOutGetDevCapsW(i, &caps, sizeof(caps));

684
        name = (i == -1) ? defaultW : caps.szPname;
685

686 687 688 689
        hr = register_codec(&CLSID_MidiRendererCategory, name,
                &CLSID_AVIMIDIRender, name, &prop_bag);
        if (FAILED(hr))
            continue;
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706

        /* write filter data */
        rgf.dwVersion = 2;
        rgf.dwMerit = (i == -1) ? MERIT_PREFERRED : MERIT_DO_NOT_USE;
        rgf.u.s2.cPins2 = 1;
        rgf.u.s2.rgPins2 = &rgpins;
        rgpins.dwFlags = REG_PINFLAG_B_RENDERER;
        rgpins.nMediaTypes = 1;
        rgpins.lpMediaType = &rgtypes;
        rgtypes.clsMajorType = &MEDIATYPE_Midi;
        rgtypes.clsMinorType = &MEDIASUBTYPE_NULL;

        write_filter_data(prop_bag, &rgf);

        /* write MidiOutId */
        V_VT(&var) = VT_I4;
        V_I4(&var) = i;
707
        IPropertyBag_Write(prop_bag, midioutidW, &var);
708 709

        VariantClear(&var);
710
        IPropertyBag_Release(prop_bag);
711 712 713
    }
}

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
static void register_vfw_codecs(void)
{
    static const WCHAR fcchandlerW[] = {'F','c','c','H','a','n','d','l','e','r',0};
    REGFILTERPINS2 rgpins[2] = {};
    IPropertyBag *prop_bag = NULL;
    REGPINTYPES rgtypes[2];
    REGFILTER2 rgf;
    GUID typeguid;
    ICINFO info;
    VARIANT var;
    HRESULT hr;
    int i = 0;
    HIC hic;

    hr = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
    if (FAILED(hr)) return;

    while (ICInfo(ICTYPE_VIDEO, i++, &info))
    {
        WCHAR name[5] = {LOBYTE(LOWORD(info.fccHandler)), HIBYTE(LOWORD(info.fccHandler)),
                         LOBYTE(HIWORD(info.fccHandler)), HIBYTE(HIWORD(info.fccHandler))};

        hic = ICOpen(ICTYPE_VIDEO, info.fccHandler, ICMODE_QUERY);
        ICGetInfo(hic, &info, sizeof(info));
        ICClose(hic);

740 741 742 743
        hr = register_codec(&CLSID_VideoCompressorCategory, name,
                &CLSID_AVICo, info.szDescription, &prop_bag);
        if (FAILED(hr))
            continue;
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

        /* write filter data */
        rgf.dwVersion = 2;
        rgf.dwMerit = MERIT_DO_NOT_USE;
        rgf.u.s2.cPins2 = 2;
        rgf.u.s2.rgPins2 = rgpins;
        rgpins[0].dwFlags = 0;
        rgpins[0].nMediaTypes = 1;
        rgpins[0].lpMediaType = &rgtypes[0];
        rgtypes[0].clsMajorType = &MEDIATYPE_Video;
        typeguid = MEDIASUBTYPE_PCM;
        typeguid.Data1 = info.fccHandler;
        rgtypes[0].clsMinorType = &typeguid;
        rgpins[1].dwFlags = REG_PINFLAG_B_OUTPUT;
        rgpins[1].nMediaTypes = 1;
        rgpins[1].lpMediaType = &rgtypes[1];
        rgtypes[1].clsMajorType = &MEDIATYPE_Video;
        rgtypes[1].clsMinorType = &GUID_NULL;

        write_filter_data(prop_bag, &rgf);

765 766 767 768 769
        /* write WaveInId */
        V_VT(&var) = VT_BSTR;
        V_BSTR(&var) = SysAllocString(name);
        IPropertyBag_Write(prop_bag, fcchandlerW, &var);

770
        VariantClear(&var);
771
        IPropertyBag_Release(prop_bag);
772 773 774
    }
}

775 776 777 778 779 780
static void register_avicap_devices(void)
{
    static const WCHAR vfwindexW[] = {'V','F','W','I','n','d','e','x',0};
    WCHAR friendlyname[] = {'v','i','d','e','o','0',0};
    IPropertyBag *prop_bag = NULL;
    WCHAR name[32], version[32];
781
    REGFILTERPINS2 rgpins = {0};
782 783 784 785 786 787 788 789 790 791 792 793
    REGPINTYPES rgtypes;
    REGFILTER2 rgf;
    VARIANT var;
    HRESULT hr;
    int i = 0;

    hr = DEVENUM_CreateAMCategoryKey(&CLSID_VideoInputDeviceCategory);
    if (FAILED(hr))
        return;

    for (i = 0; i < 10; ++i)
    {
794
        if (!capGetDriverDescriptionW(i, name, ARRAY_SIZE(name), version, ARRAY_SIZE(version)))
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
            break;

        friendlyname[5] = '0' + i;

        hr = register_codec(&CLSID_VideoInputDeviceCategory, name,
                &CLSID_VfwCapture, friendlyname, &prop_bag);
        if (FAILED(hr))
            continue;

        rgf.dwVersion = 2;
        rgf.dwMerit = MERIT_DO_NOT_USE;
        rgf.u.s2.cPins2 = 1;
        rgf.u.s2.rgPins2 = &rgpins;
        rgpins.dwFlags = 0;
        rgpins.nMediaTypes = 1;
        rgpins.lpMediaType = &rgtypes;
        rgtypes.clsMajorType = &MEDIATYPE_Video;
        rgtypes.clsMinorType = &MEDIASUBTYPE_None;

        write_filter_data(prop_bag, &rgf);

        /* write VFWIndex */
        V_VT(&var) = VT_I4;
        V_I4(&var) = i;
        IPropertyBag_Write(prop_bag, vfwindexW, &var);

        VariantClear(&var);
        IPropertyBag_Release(prop_bag);
    }
}

826 827 828
/**********************************************************************
 * DEVENUM_ICreateDevEnum_CreateClassEnumerator
 */
829
static HRESULT WINAPI DEVENUM_ICreateDevEnum_CreateClassEnumerator(
830
    ICreateDevEnum *iface, REFCLSID class, IEnumMoniker **out, DWORD flags)
831
{
832
    WCHAR guidstr[CHARS_IN_GUID];
833
    HRESULT hr;
834
    HKEY key;
835

836
    TRACE("iface %p, class %s, out %p, flags %#x.\n", iface, debugstr_guid(class), out, flags);
837

838
    if (!out)
839 840
        return E_POINTER;

841
    *out = NULL;
842

843 844 845 846 847
    if (!RegOpenKeyW(HKEY_CURRENT_USER, wszActiveMovieKey, &key))
    {
        StringFromGUID2(class, guidstr, ARRAY_SIZE(guidstr));
        RegDeleteTreeW(key, guidstr);
    }
848

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
    if (IsEqualGUID(class, &CLSID_LegacyAmFilterCategory))
        register_legacy_filters();
    else if (IsEqualGUID(class, &CLSID_AudioRendererCategory))
    {
        hr = DirectSoundEnumerateW(&register_dsound_devices, NULL);
        if (FAILED(hr)) return hr;
        register_waveout_devices();
        register_midiout_devices();
    }
    else if (IsEqualGUID(class, &CLSID_AudioInputDeviceCategory))
        register_wavein_devices();
    else if (IsEqualGUID(class, &CLSID_VideoCompressorCategory))
        register_vfw_codecs();
    else if (IsEqualGUID(class, &CLSID_VideoInputDeviceCategory))
        register_avicap_devices();

    return create_EnumMoniker(class, out);
866 867 868 869 870
}

/**********************************************************************
 * ICreateDevEnum_Vtbl
 */
871
static const ICreateDevEnumVtbl ICreateDevEnum_Vtbl =
872 873 874 875 876 877 878 879 880 881
{
    DEVENUM_ICreateDevEnum_QueryInterface,
    DEVENUM_ICreateDevEnum_AddRef,
    DEVENUM_ICreateDevEnum_Release,
    DEVENUM_ICreateDevEnum_CreateClassEnumerator,
};

/**********************************************************************
 * static CreateDevEnum instance
 */
882
ICreateDevEnum DEVENUM_CreateDevEnum = { &ICreateDevEnum_Vtbl };
883

884 885 886 887 888 889 890 891 892 893 894 895 896 897
/**********************************************************************
 * DEVENUM_CreateAMCategoryKey (INTERNAL)
 *
 * Creates a registry key for a category at HKEY_CURRENT_USER\Software\
 * Microsoft\ActiveMovie\devenum\{clsid}
 */
static HRESULT DEVENUM_CreateAMCategoryKey(const CLSID * clsidCategory)
{
    WCHAR wszRegKey[MAX_PATH];
    HRESULT res = S_OK;
    HKEY hkeyDummy = NULL;

    strcpyW(wszRegKey, wszActiveMovieKey);

898
    if (!StringFromGUID2(clsidCategory, wszRegKey + strlenW(wszRegKey), ARRAY_SIZE(wszRegKey) - strlenW(wszRegKey)))
899 900 901
        res = E_INVALIDARG;

    if (SUCCEEDED(res))
902 903 904 905
    {
        LONG lRes = RegCreateKeyW(HKEY_CURRENT_USER, wszRegKey, &hkeyDummy);
        res = HRESULT_FROM_WIN32(lRes);
    }
906 907 908 909 910 911 912 913 914

    if (hkeyDummy)
        RegCloseKey(hkeyDummy);

    if (FAILED(res))
        ERR("Failed to create key HKEY_CURRENT_USER\\%s\n", debugstr_w(wszRegKey));

    return res;
}