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