urlmon: Added TYMED_FILE support to CopyStgMedium.
[wine] / dlls / dsound / dsound_main.c
1 /*                      DirectSound
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998 Rob Riggs
5  * Copyright 2000-2002 TransGaming Technologies, Inc.
6  *
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.
11  *
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.
16  *
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
20  *
21  * Most thread locking is complete. There may be a few race
22  * conditions still lurking.
23  *
24  * TODO:
25  *      Implement SetCooperativeLevel properly (need to address focus issues)
26  *      Implement DirectSound3DBuffers (stubs in place)
27  *      Use hardware 3D support if available
28  *      Add critical section locking inside Release and AddRef methods
29  *      Handle static buffers - put those in hardware, non-static not in hardware
30  *      Hardware DuplicateSoundBuffer
31  *      Proper volume calculation for 3d buffers
32  *      Remove DS_HEL_FRAGS and use mixer fragment length for it
33  */
34
35 #include <stdarg.h>
36
37 #define COBJMACROS
38 #define NONAMELESSSTRUCT
39 #define NONAMELESSUNION
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "winreg.h"
45 #include "mmsystem.h"
46 #include "winternl.h"
47 #include "mmddk.h"
48 #include "wine/debug.h"
49 #include "dsound.h"
50 #include "dsconf.h"
51 #include "ks.h"
52 #include "initguid.h"
53 #include "ksmedia.h"
54 #include "dsdriver.h"
55
56 #include "dsound_private.h"
57
58 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
59
60 #define DS_HEL_BUFLEN 0x8000 /* HEL: The buffer length of the emulated buffer */
61 #define DS_SND_QUEUE_MAX 10 /* max number of fragments to prebuffer, each fragment is approximately 10 ms long */
62 #define DS_SND_QUEUE_MIN 6 /* If the minimum of prebuffered fragments go below this, forcibly take all locks to prevent underruns */
63
64 DirectSoundDevice*      DSOUND_renderer[MAXWAVEDRIVERS];
65 GUID                    DSOUND_renderer_guids[MAXWAVEDRIVERS];
66 GUID                    DSOUND_capture_guids[MAXWAVEDRIVERS];
67
68 HRESULT mmErr(UINT err)
69 {
70         switch(err) {
71         case MMSYSERR_NOERROR:
72                 return DS_OK;
73         case MMSYSERR_ALLOCATED:
74                 return DSERR_ALLOCATED;
75         case MMSYSERR_ERROR:
76         case MMSYSERR_INVALHANDLE:
77         case WAVERR_STILLPLAYING:
78                 return DSERR_GENERIC; /* FIXME */
79         case MMSYSERR_NODRIVER:
80                 return DSERR_NODRIVER;
81         case MMSYSERR_NOMEM:
82                 return DSERR_OUTOFMEMORY;
83         case MMSYSERR_INVALPARAM:
84         case WAVERR_BADFORMAT:
85         case WAVERR_UNPREPARED:
86                 return DSERR_INVALIDPARAM;
87         case MMSYSERR_NOTSUPPORTED:
88                 return DSERR_UNSUPPORTED;
89         default:
90                 FIXME("Unknown MMSYS error %d\n",err);
91                 return DSERR_GENERIC;
92         }
93 }
94
95 int ds_emuldriver = 0;
96 int ds_hel_buflen = DS_HEL_BUFLEN;
97 int ds_snd_queue_max = DS_SND_QUEUE_MAX;
98 int ds_snd_queue_min = DS_SND_QUEUE_MIN;
99 int ds_hw_accel = DS_HW_ACCEL_FULL;
100 int ds_default_playback = 0;
101 int ds_default_capture = 0;
102 int ds_default_sample_rate = 44100;
103 int ds_default_bits_per_sample = 16;
104
105 /*
106  * Get a config key from either the app-specific or the default config
107  */
108
109 static inline DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
110                                     char *buffer, DWORD size )
111 {
112     if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
113     if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
114     return ERROR_FILE_NOT_FOUND;
115 }
116
117
118 /*
119  * Setup the dsound options.
120  */
121
122 void setup_dsound_options(void)
123 {
124     char buffer[MAX_PATH+16];
125     HKEY hkey, appkey = 0;
126     DWORD len;
127
128     buffer[MAX_PATH]='\0';
129
130     /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
131     if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectSound", &hkey )) hkey = 0;
132
133     len = GetModuleFileNameA( 0, buffer, MAX_PATH );
134     if (len && len < MAX_PATH)
135     {
136         HKEY tmpkey;
137         /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
138         if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
139         {
140             char *p, *appname = buffer;
141             if ((p = strrchr( appname, '/' ))) appname = p + 1;
142             if ((p = strrchr( appname, '\\' ))) appname = p + 1;
143             strcat( appname, "\\DirectSound" );
144             TRACE("appname = [%s]\n", appname);
145             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
146             RegCloseKey( tmpkey );
147         }
148     }
149
150     /* get options */
151
152     if (!get_config_key( hkey, appkey, "EmulDriver", buffer, MAX_PATH ))
153         ds_emuldriver = strcmp(buffer, "N");
154
155     if (!get_config_key( hkey, appkey, "HelBuflen", buffer, MAX_PATH ))
156         ds_hel_buflen = atoi(buffer);
157
158     if (!get_config_key( hkey, appkey, "SndQueueMax", buffer, MAX_PATH ))
159         ds_snd_queue_max = atoi(buffer);
160
161     if (!get_config_key( hkey, appkey, "SndQueueMin", buffer, MAX_PATH ))
162         ds_snd_queue_min = atoi(buffer);
163
164     if (!get_config_key( hkey, appkey, "HardwareAcceleration", buffer, MAX_PATH )) {
165         if (strcmp(buffer, "Full") == 0)
166             ds_hw_accel = DS_HW_ACCEL_FULL;
167         else if (strcmp(buffer, "Standard") == 0)
168             ds_hw_accel = DS_HW_ACCEL_STANDARD;
169         else if (strcmp(buffer, "Basic") == 0)
170             ds_hw_accel = DS_HW_ACCEL_BASIC;
171         else if (strcmp(buffer, "Emulation") == 0)
172             ds_hw_accel = DS_HW_ACCEL_EMULATION;
173     }
174
175     if (!get_config_key( hkey, appkey, "DefaultPlayback", buffer, MAX_PATH ))
176             ds_default_playback = atoi(buffer);
177
178     if (!get_config_key( hkey, appkey, "DefaultCapture", buffer, MAX_PATH ))
179             ds_default_capture = atoi(buffer);
180
181     if (!get_config_key( hkey, appkey, "DefaultSampleRate", buffer, MAX_PATH ))
182             ds_default_sample_rate = atoi(buffer);
183
184     if (!get_config_key( hkey, appkey, "DefaultBitsPerSample", buffer, MAX_PATH ))
185             ds_default_bits_per_sample = atoi(buffer);
186
187     if (appkey) RegCloseKey( appkey );
188     if (hkey) RegCloseKey( hkey );
189
190     if (ds_emuldriver)
191        WARN("ds_emuldriver = %d (default=0)\n",ds_emuldriver);
192     if (ds_hel_buflen != DS_HEL_BUFLEN)
193        WARN("ds_hel_buflen = %d (default=%d)\n",ds_hel_buflen ,DS_HEL_BUFLEN);
194     if (ds_snd_queue_max != DS_SND_QUEUE_MAX)
195        WARN("ds_snd_queue_max = %d (default=%d)\n",ds_snd_queue_max ,DS_SND_QUEUE_MAX);
196     if (ds_snd_queue_min != DS_SND_QUEUE_MIN)
197        WARN("ds_snd_queue_min = %d (default=%d)\n",ds_snd_queue_min ,DS_SND_QUEUE_MIN);
198     if (ds_hw_accel != DS_HW_ACCEL_FULL)
199         WARN("ds_hw_accel = %s (default=Full)\n",
200             ds_hw_accel==DS_HW_ACCEL_FULL ? "Full" :
201             ds_hw_accel==DS_HW_ACCEL_STANDARD ? "Standard" :
202             ds_hw_accel==DS_HW_ACCEL_BASIC ? "Basic" :
203             ds_hw_accel==DS_HW_ACCEL_EMULATION ? "Emulation" :
204             "Unknown");
205     if (ds_default_playback != 0)
206         WARN("ds_default_playback = %d (default=0)\n",ds_default_playback);
207     if (ds_default_capture != 0)
208         WARN("ds_default_capture = %d (default=0)\n",ds_default_playback);
209     if (ds_default_sample_rate != 44100)
210         WARN("ds_default_sample_rate = %d (default=44100)\n",ds_default_sample_rate);
211     if (ds_default_bits_per_sample != 16)
212         WARN("ds_default_bits_per_sample = %d (default=16)\n",ds_default_bits_per_sample);
213 }
214
215 static const char * get_device_id(LPCGUID pGuid)
216 {
217     if (IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
218         return "DSDEVID_DefaultPlayback";
219     else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
220         return "DSDEVID_DefaultVoicePlayback";
221     else if (IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
222         return "DSDEVID_DefaultCapture";
223     else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
224         return "DSDEVID_DefaultVoiceCapture";
225     return debugstr_guid(pGuid);
226 }
227
228 /***************************************************************************
229  * GetDeviceID  [DSOUND.9]
230  *
231  * Retrieves unique identifier of default device specified
232  *
233  * PARAMS
234  *    pGuidSrc  [I] Address of device GUID.
235  *    pGuidDest [O] Address to receive unique device GUID.
236  *
237  * RETURNS
238  *    Success: DS_OK
239  *    Failure: DSERR_INVALIDPARAM
240  *
241  * NOTES
242  *    pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
243  *    DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
244  *    DSDEVID_DefaultVoiceCapture.
245  *    Returns pGuidSrc if pGuidSrc is a valid device or the device
246  *    GUID for the specified constants.
247  */
248 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
249 {
250     TRACE("(%s,%p)\n", get_device_id(pGuidSrc),pGuidDest);
251
252     if ( pGuidSrc == NULL) {
253         WARN("invalid parameter: pGuidSrc == NULL\n");
254         return DSERR_INVALIDPARAM;
255     }
256
257     if ( pGuidDest == NULL ) {
258         WARN("invalid parameter: pGuidDest == NULL\n");
259         return DSERR_INVALIDPARAM;
260     }
261
262     if ( IsEqualGUID( &DSDEVID_DefaultPlayback, pGuidSrc ) ||
263          IsEqualGUID( &DSDEVID_DefaultVoicePlayback, pGuidSrc ) ) {
264         CopyMemory(pGuidDest, &DSOUND_renderer_guids[ds_default_playback], sizeof(GUID));
265         TRACE("returns %s\n", get_device_id(pGuidDest));
266         return DS_OK;
267     }
268
269     if ( IsEqualGUID( &DSDEVID_DefaultCapture, pGuidSrc ) ||
270          IsEqualGUID( &DSDEVID_DefaultVoiceCapture, pGuidSrc ) ) {
271         CopyMemory(pGuidDest, &DSOUND_capture_guids[ds_default_capture], sizeof(GUID));
272         TRACE("returns %s\n", get_device_id(pGuidDest));
273         return DS_OK;
274     }
275
276     CopyMemory(pGuidDest, pGuidSrc, sizeof(GUID));
277     TRACE("returns %s\n", get_device_id(pGuidDest));
278
279     return DS_OK;
280 }
281
282
283 /***************************************************************************
284  * DirectSoundEnumerateA [DSOUND.2]
285  *
286  * Enumerate all DirectSound drivers installed in the system
287  *
288  * PARAMS
289  *    lpDSEnumCallback  [I] Address of callback function.
290  *    lpContext         [I] Address of user defined context passed to callback function.
291  *
292  * RETURNS
293  *    Success: DS_OK
294  *    Failure: DSERR_INVALIDPARAM
295  */
296 HRESULT WINAPI DirectSoundEnumerateA(
297     LPDSENUMCALLBACKA lpDSEnumCallback,
298     LPVOID lpContext)
299 {
300     unsigned devs, wod;
301     DSDRIVERDESC desc;
302     GUID guid;
303     int err;
304
305     TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
306         lpDSEnumCallback, lpContext);
307
308     if (lpDSEnumCallback == NULL) {
309         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
310         return DSERR_INVALIDPARAM;
311     }
312
313     devs = waveOutGetNumDevs();
314     if (devs > 0) {
315         if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
316             for (wod = 0; wod < devs; ++wod) {
317                 if (IsEqualGUID( &guid, &DSOUND_renderer_guids[wod]) ) {
318                     err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
319                     if (err == DS_OK) {
320                         TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
321                               "Primary Sound Driver",desc.szDrvname,lpContext);
322                         if (lpDSEnumCallback(NULL, "Primary Sound Driver", desc.szDrvname, lpContext) == FALSE)
323                             return DS_OK;
324                     }
325                 }
326             }
327         }
328     }
329
330     for (wod = 0; wod < devs; ++wod) {
331         err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
332         if (err == DS_OK) {
333             TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
334                   debugstr_guid(&DSOUND_renderer_guids[wod]),desc.szDesc,desc.szDrvname,lpContext);
335             if (lpDSEnumCallback(&DSOUND_renderer_guids[wod], desc.szDesc, desc.szDrvname, lpContext) == FALSE)
336                 return DS_OK;
337         }
338     }
339     return DS_OK;
340 }
341
342 /***************************************************************************
343  * DirectSoundEnumerateW [DSOUND.3]
344  *
345  * Enumerate all DirectSound drivers installed in the system
346  *
347  * PARAMS
348  *    lpDSEnumCallback  [I] Address of callback function.
349  *    lpContext         [I] Address of user defined context passed to callback function.
350  *
351  * RETURNS
352  *    Success: DS_OK
353  *    Failure: DSERR_INVALIDPARAM
354  */
355 HRESULT WINAPI DirectSoundEnumerateW(
356         LPDSENUMCALLBACKW lpDSEnumCallback,
357         LPVOID lpContext )
358 {
359     unsigned devs, wod;
360     DSDRIVERDESC desc;
361     GUID guid;
362     int err;
363     WCHAR wDesc[MAXPNAMELEN];
364     WCHAR wName[MAXPNAMELEN];
365
366     TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
367         lpDSEnumCallback, lpContext);
368
369     if (lpDSEnumCallback == NULL) {
370         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
371         return DSERR_INVALIDPARAM;
372     }
373
374     devs = waveOutGetNumDevs();
375     if (devs > 0) {
376         if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
377             for (wod = 0; wod < devs; ++wod) {
378                 if (IsEqualGUID( &guid, &DSOUND_renderer_guids[wod] ) ) {
379                     err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
380                     if (err == DS_OK) {
381                         TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
382                               "Primary Sound Driver",desc.szDrvname,lpContext);
383                         MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
384                                              wDesc, sizeof(wDesc)/sizeof(WCHAR) );
385                         MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
386                                              wName, sizeof(wName)/sizeof(WCHAR) );
387                         if (lpDSEnumCallback(NULL, wDesc, wName, lpContext) == FALSE)
388                             return DS_OK;
389                     }
390                 }
391             }
392         }
393     }
394
395     for (wod = 0; wod < devs; ++wod) {
396         err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
397         if (err == DS_OK) {
398             TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
399                   debugstr_guid(&DSOUND_renderer_guids[wod]),desc.szDesc,desc.szDrvname,lpContext);
400             MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
401                                  wDesc, sizeof(wDesc)/sizeof(WCHAR) );
402             MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
403                                  wName, sizeof(wName)/sizeof(WCHAR) );
404             if (lpDSEnumCallback(&DSOUND_renderer_guids[wod], wDesc, wName, lpContext) == FALSE)
405                 return DS_OK;
406         }
407     }
408     return DS_OK;
409 }
410
411 /*******************************************************************************
412  * DirectSound ClassFactory
413  */
414
415 typedef  HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
416  
417 typedef struct {
418     const IClassFactoryVtbl *lpVtbl;
419     LONG ref;
420     REFCLSID rclsid;
421     FnCreateInstance pfnCreateInstance;
422 } IClassFactoryImpl;
423
424 static HRESULT WINAPI
425 DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
426 {
427     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
428     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
429     if (ppobj == NULL)
430         return E_POINTER;
431     if (IsEqualIID(riid, &IID_IUnknown) ||
432         IsEqualIID(riid, &IID_IClassFactory))
433     {
434         *ppobj = iface;
435         IUnknown_AddRef(iface);
436         return S_OK;
437     }
438     *ppobj = NULL;
439     return E_NOINTERFACE;
440 }
441
442 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
443 {
444     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
445     ULONG ref = InterlockedIncrement(&(This->ref));
446     TRACE("(%p) ref was %d\n", This, ref - 1);
447     return ref;
448 }
449
450 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
451 {
452     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
453     ULONG ref = InterlockedDecrement(&(This->ref));
454     TRACE("(%p) ref was %d\n", This, ref + 1);
455     /* static class, won't be freed */
456     return ref;
457 }
458
459 static HRESULT WINAPI DSCF_CreateInstance(
460     LPCLASSFACTORY iface,
461     LPUNKNOWN pOuter,
462     REFIID riid,
463     LPVOID *ppobj)
464 {
465     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
466     TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
467
468     if (pOuter)
469         return CLASS_E_NOAGGREGATION;
470
471     if (ppobj == NULL) {
472         WARN("invalid parameter\n");
473         return DSERR_INVALIDPARAM;
474     }
475     *ppobj = NULL;
476     return This->pfnCreateInstance(riid, ppobj);
477 }
478  
479 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
480 {
481     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
482     FIXME("(%p, %d) stub!\n", This, dolock);
483     return S_OK;
484 }
485
486 static const IClassFactoryVtbl DSCF_Vtbl = {
487     DSCF_QueryInterface,
488     DSCF_AddRef,
489     DSCF_Release,
490     DSCF_CreateInstance,
491     DSCF_LockServer
492 };
493
494 static IClassFactoryImpl DSOUND_CF[] = {
495     { &DSCF_Vtbl, 1, &CLSID_DirectSound, (FnCreateInstance)DSOUND_Create },
496     { &DSCF_Vtbl, 1, &CLSID_DirectSound8, (FnCreateInstance)DSOUND_Create8 },
497     { &DSCF_Vtbl, 1, &CLSID_DirectSoundCapture, (FnCreateInstance)DSOUND_CaptureCreate },
498     { &DSCF_Vtbl, 1, &CLSID_DirectSoundCapture8, (FnCreateInstance)DSOUND_CaptureCreate8 },
499     { &DSCF_Vtbl, 1, &CLSID_DirectSoundFullDuplex, (FnCreateInstance)DSOUND_FullDuplexCreate },
500     { &DSCF_Vtbl, 1, &CLSID_DirectSoundPrivate, (FnCreateInstance)IKsPrivatePropertySetImpl_Create },
501     { NULL, 0, NULL, NULL }
502 };
503
504 /*******************************************************************************
505  * DllGetClassObject [DSOUND.@]
506  * Retrieves class object from a DLL object
507  *
508  * NOTES
509  *    Docs say returns STDAPI
510  *
511  * PARAMS
512  *    rclsid [I] CLSID for the class object
513  *    riid   [I] Reference to identifier of interface for class object
514  *    ppv    [O] Address of variable to receive interface pointer for riid
515  *
516  * RETURNS
517  *    Success: S_OK
518  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
519  *             E_UNEXPECTED
520  */
521 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
522 {
523     int i = 0;
524     TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
525
526     if (ppv == NULL) {
527         WARN("invalid parameter\n");
528         return E_INVALIDARG;
529     }
530
531     *ppv = NULL;
532
533     if (!IsEqualIID(riid, &IID_IClassFactory) &&
534         !IsEqualIID(riid, &IID_IUnknown)) {
535         WARN("no interface for %s\n", debugstr_guid(riid));
536         return E_NOINTERFACE;
537     }
538
539     while (NULL != DSOUND_CF[i].rclsid) {
540         if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
541             DSCF_AddRef((IClassFactory*) &DSOUND_CF[i]);
542             *ppv = &DSOUND_CF[i];
543             return S_OK;
544         }
545         i++;
546     }
547
548     WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
549          debugstr_guid(riid), ppv);
550     return CLASS_E_CLASSNOTAVAILABLE;
551 }
552
553
554 /*******************************************************************************
555  * DllCanUnloadNow [DSOUND.4]
556  * Determines whether the DLL is in use.
557  *
558  * RETURNS
559  *    Success: S_OK
560  *    Failure: S_FALSE
561  */
562 HRESULT WINAPI DllCanUnloadNow(void)
563 {
564     FIXME("(void): stub\n");
565     return S_FALSE;
566 }
567
568 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)      \
569         guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2;               \
570         guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3;     \
571         guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6;     \
572         guid.Data4[6] = b7; guid.Data4[7] = b8;
573
574 /***********************************************************************
575  *           DllMain (DSOUND.init)
576  */
577 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
578 {
579     int i;
580     TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpvReserved);
581
582     switch (fdwReason) {
583     case DLL_PROCESS_ATTACH:
584         TRACE("DLL_PROCESS_ATTACH\n");
585         for (i = 0; i < MAXWAVEDRIVERS; i++) {
586             DSOUND_renderer[i] = NULL;
587             DSOUND_capture[i] = NULL;
588             INIT_GUID(DSOUND_renderer_guids[i], 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
589             INIT_GUID(DSOUND_capture_guids[i],  0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
590         }
591         DisableThreadLibraryCalls(hInstDLL);
592         /* Increase refcount on dsound by 1 */
593         GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
594         break;
595     case DLL_PROCESS_DETACH:
596         TRACE("DLL_PROCESS_DETACH\n");
597         break;
598     default:
599         TRACE("UNKNOWN REASON\n");
600         break;
601     }
602     return TRUE;
603 }