ole32: Fix infinite recursion in CompositeMonikerImpl_BindToStorage.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 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 static HRESULT WINAPI
430 DSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
431         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
432
433         FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
434         return E_NOINTERFACE;
435 }
436
437 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
438 {
439     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
440     ULONG ref = InterlockedIncrement(&(This->ref));
441     TRACE("(%p) ref was %ld\n", This, ref - 1);
442     return ref;
443 }
444
445 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
446 {
447     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
448     ULONG ref = InterlockedDecrement(&(This->ref));
449     TRACE("(%p) ref was %ld\n", This, ref + 1);
450     /* static class, won't be freed */
451     return ref;
452 }
453
454 static HRESULT WINAPI DSCF_CreateInstance(
455         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
456 ) {
457         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
458         TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
459
460         if (pOuter)
461                 return CLASS_E_NOAGGREGATION;
462
463         if (ppobj == NULL) {
464                 WARN("invalid parameter\n");
465                 return DSERR_INVALIDPARAM;
466         }
467
468         *ppobj = NULL;
469
470         if ( IsEqualIID( &IID_IDirectSound, riid ) )
471                 return DSOUND_Create((LPDIRECTSOUND*)ppobj,pOuter);
472
473         if ( IsEqualIID( &IID_IDirectSound8, riid ) )
474                 return DSOUND_Create8((LPDIRECTSOUND8*)ppobj,pOuter);
475
476         WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);     
477         return E_NOINTERFACE;
478 }
479
480 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
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 = { &DSCF_Vtbl, 1 };
495
496 /*******************************************************************************
497  * DirectSoundPrivate ClassFactory
498  */
499
500 static HRESULT WINAPI
501 DSPCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
502         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
503
504         FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
505         return E_NOINTERFACE;
506 }
507
508 static ULONG WINAPI DSPCF_AddRef(LPCLASSFACTORY iface)
509 {
510     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
511     ULONG ref = InterlockedIncrement(&(This->ref));
512     TRACE("(%p) ref was %ld\n", This, ref - 1);
513     return ref;
514 }
515
516 static ULONG WINAPI DSPCF_Release(LPCLASSFACTORY iface)
517 {
518     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
519     ULONG ref = InterlockedDecrement(&(This->ref));
520     TRACE("(%p) ref was %ld\n", This, ref + 1);
521     /* static class, won't be freed */
522     return ref;
523 }
524
525 static HRESULT WINAPI
526 DSPCF_CreateInstance(
527         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
528 ) {
529         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
530         TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
531
532         if (ppobj == NULL) {
533                 WARN("invalid parameter\n");
534                 return DSERR_INVALIDPARAM;
535         }
536
537         *ppobj = NULL;
538
539         if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
540                 return IKsPrivatePropertySetImpl_Create((IKsPrivatePropertySetImpl**)ppobj);
541         }
542
543         WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);     
544         return E_NOINTERFACE;
545 }
546
547 static HRESULT WINAPI
548 DSPCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
549         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
550         FIXME("(%p)->(%d),stub!\n",This,dolock);
551         return S_OK;
552 }
553
554 static const IClassFactoryVtbl DSPCF_Vtbl = {
555         DSPCF_QueryInterface,
556         DSPCF_AddRef,
557         DSPCF_Release,
558         DSPCF_CreateInstance,
559         DSPCF_LockServer
560 };
561
562 static IClassFactoryImpl DSOUND_PRIVATE_CF = { &DSPCF_Vtbl, 1 };
563
564 /*******************************************************************************
565  * DllGetClassObject [DSOUND.@]
566  * Retrieves class object from a DLL object
567  *
568  * NOTES
569  *    Docs say returns STDAPI
570  *
571  * PARAMS
572  *    rclsid [I] CLSID for the class object
573  *    riid   [I] Reference to identifier of interface for class object
574  *    ppv    [O] Address of variable to receive interface pointer for riid
575  *
576  * RETURNS
577  *    Success: S_OK
578  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
579  *             E_UNEXPECTED
580  */
581 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
582 {
583     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
584
585     if (ppv == NULL) {
586         WARN("invalid parameter\n");
587         return E_INVALIDARG;
588     }
589
590     *ppv = NULL;
591
592     if ( IsEqualCLSID( &CLSID_DirectSound, rclsid ) ||
593          IsEqualCLSID( &CLSID_DirectSound8, rclsid ) ) {
594         if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
595             *ppv = (LPVOID)&DSOUND_CF;
596             IClassFactory_AddRef((IClassFactory*)*ppv);
597             return S_OK;
598         }
599         WARN("(%s,%s,%p): no interface found.\n",
600             debugstr_guid(rclsid), debugstr_guid(riid), ppv);
601         return S_FALSE;
602     }
603
604     if ( IsEqualCLSID( &CLSID_DirectSoundCapture, rclsid ) ||
605          IsEqualCLSID( &CLSID_DirectSoundCapture8, rclsid ) ) {
606         if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
607             *ppv = (LPVOID)&DSOUND_CAPTURE_CF;
608             IClassFactory_AddRef((IClassFactory*)*ppv);
609             return S_OK;
610         }
611         WARN("(%s,%s,%p): no interface found.\n",
612             debugstr_guid(rclsid), debugstr_guid(riid), ppv);
613         return S_FALSE;
614     }
615
616     if ( IsEqualCLSID( &CLSID_DirectSoundFullDuplex, rclsid ) ) {
617         if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
618             *ppv = (LPVOID)&DSOUND_FULLDUPLEX_CF;
619             IClassFactory_AddRef((IClassFactory*)*ppv);
620             return S_OK;
621         }
622         WARN("(%s,%s,%p): no interface found.\n",
623             debugstr_guid(rclsid), debugstr_guid(riid), ppv);
624         return S_FALSE;
625     }
626
627     if ( IsEqualCLSID( &CLSID_DirectSoundPrivate, rclsid ) ) {
628         if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
629             *ppv = (LPVOID)&DSOUND_PRIVATE_CF;
630             IClassFactory_AddRef((IClassFactory*)*ppv);
631             return S_OK;
632         }
633         WARN("(%s,%s,%p): no interface found.\n",
634             debugstr_guid(rclsid), debugstr_guid(riid), ppv);
635         return S_FALSE;
636     }
637
638     WARN("(%s,%s,%p): no class found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
639     return CLASS_E_CLASSNOTAVAILABLE;
640 }
641
642
643 /*******************************************************************************
644  * DllCanUnloadNow [DSOUND.4]
645  * Determines whether the DLL is in use.
646  *
647  * RETURNS
648  *    Success: S_OK
649  *    Failure: S_FALSE
650  */
651 HRESULT WINAPI DllCanUnloadNow(void)
652 {
653     FIXME("(void): stub\n");
654     return S_FALSE;
655 }
656
657 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)      \
658         guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2;               \
659         guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3;     \
660         guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6;     \
661         guid.Data4[6] = b7; guid.Data4[7] = b8;
662
663 /***********************************************************************
664  *           DllMain (DSOUND.init)
665  */
666 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
667 {
668     int i;
669     TRACE("(%p %ld %p)\n", hInstDLL, fdwReason, lpvReserved);
670
671     switch (fdwReason) {
672     case DLL_PROCESS_ATTACH:
673         TRACE("DLL_PROCESS_ATTACH\n");
674         for (i = 0; i < MAXWAVEDRIVERS; i++) {
675             DSOUND_renderer[i] = NULL;
676             DSOUND_capture[i] = NULL;
677             INIT_GUID(DSOUND_renderer_guids[i], 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
678             INIT_GUID(DSOUND_capture_guids[i],  0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
679         }
680         break;
681     case DLL_PROCESS_DETACH:
682         TRACE("DLL_PROCESS_DETACH\n");
683         break;
684     case DLL_THREAD_ATTACH:
685         TRACE("DLL_THREAD_ATTACH\n");
686         break;
687     case DLL_THREAD_DETACH:
688         TRACE("DLL_THREAD_DETACH\n");
689         break;
690     default:
691         TRACE("UNKNOWN REASON\n");
692         break;
693     }
694     return TRUE;
695 }