dsound: Fix double free bug in tests.
[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
112 /*
113  * Get a config key from either the app-specific or the default config
114  */
115
116 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
117                                     char *buffer, DWORD size )
118 {
119     if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
120     if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
121     return ERROR_FILE_NOT_FOUND;
122 }
123
124
125 /*
126  * Setup the dsound options.
127  */
128
129 void setup_dsound_options(void)
130 {
131     char buffer[MAX_PATH+16];
132     HKEY hkey, appkey = 0;
133     DWORD len;
134
135     buffer[MAX_PATH]='\0';
136
137     /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
138     if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectSound", &hkey )) hkey = 0;
139
140     len = GetModuleFileNameA( 0, buffer, MAX_PATH );
141     if (len && len < MAX_PATH)
142     {
143         HKEY tmpkey;
144         /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
145         if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
146         {
147             char *p, *appname = buffer;
148             if ((p = strrchr( appname, '/' ))) appname = p + 1;
149             if ((p = strrchr( appname, '\\' ))) appname = p + 1;
150             strcat( appname, "\\DirectSound" );
151             TRACE("appname = [%s]\n", appname);
152             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
153             RegCloseKey( tmpkey );
154         }
155     }
156
157     /* get options */
158
159     if (!get_config_key( hkey, appkey, "EmulDriver", buffer, MAX_PATH ))
160         ds_emuldriver = strcmp(buffer, "N");
161
162     if (!get_config_key( hkey, appkey, "HELmargin", buffer, MAX_PATH ))
163         ds_hel_margin = atoi(buffer);
164
165     if (!get_config_key( hkey, appkey, "HELqueue", buffer, MAX_PATH ))
166         ds_hel_queue = atoi(buffer);
167
168     if (!get_config_key( hkey, appkey, "SndQueueMax", buffer, MAX_PATH ))
169         ds_snd_queue_max = atoi(buffer);
170
171     if (!get_config_key( hkey, appkey, "SndQueueMin", buffer, MAX_PATH ))
172         ds_snd_queue_min = atoi(buffer);
173
174     if (!get_config_key( hkey, appkey, "HardwareAcceleration", buffer, MAX_PATH )) {
175         if (strcmp(buffer, "Full") == 0)
176             ds_hw_accel = DS_HW_ACCEL_FULL;
177         else if (strcmp(buffer, "Standard") == 0)
178             ds_hw_accel = DS_HW_ACCEL_STANDARD;
179         else if (strcmp(buffer, "Basic") == 0)
180             ds_hw_accel = DS_HW_ACCEL_BASIC;
181         else if (strcmp(buffer, "Emulation") == 0)
182             ds_hw_accel = DS_HW_ACCEL_EMULATION;
183     }
184
185     if (!get_config_key( hkey, appkey, "DefaultPlayback", buffer, MAX_PATH ))
186             ds_default_playback = atoi(buffer);
187
188     if (!get_config_key( hkey, appkey, "DefaultCapture", buffer, MAX_PATH ))
189             ds_default_capture = atoi(buffer);
190
191     if (appkey) RegCloseKey( appkey );
192     if (hkey) RegCloseKey( hkey );
193
194     if (ds_emuldriver != DS_EMULDRIVER )
195        WARN("ds_emuldriver = %d (default=%d)\n",ds_emuldriver, DS_EMULDRIVER);
196     if (ds_hel_margin != DS_HEL_MARGIN )
197        WARN("ds_hel_margin = %d (default=%d)\n",ds_hel_margin, DS_HEL_MARGIN );
198     if (ds_hel_queue != DS_HEL_QUEUE )
199        WARN("ds_hel_queue = %d (default=%d)\n",ds_hel_queue, DS_HEL_QUEUE );
200     if (ds_snd_queue_max != DS_SND_QUEUE_MAX)
201        WARN("ds_snd_queue_max = %d (default=%d)\n",ds_snd_queue_max ,DS_SND_QUEUE_MAX);
202     if (ds_snd_queue_min != DS_SND_QUEUE_MIN)
203        WARN("ds_snd_queue_min = %d (default=%d)\n",ds_snd_queue_min ,DS_SND_QUEUE_MIN);
204     if (ds_hw_accel != DS_HW_ACCEL_FULL)
205         WARN("ds_hw_accel = %s (default=Full)\n",
206             ds_hw_accel==DS_HW_ACCEL_FULL ? "Full" :
207             ds_hw_accel==DS_HW_ACCEL_STANDARD ? "Standard" :
208             ds_hw_accel==DS_HW_ACCEL_BASIC ? "Basic" :
209             ds_hw_accel==DS_HW_ACCEL_EMULATION ? "Emulation" :
210             "Unknown");
211     if (ds_default_playback != 0)
212         WARN("ds_default_playback = %d (default=0)\n",ds_default_playback);
213     if (ds_default_capture != 0)
214         WARN("ds_default_capture = %d (default=0)\n",ds_default_playback);
215 }
216
217 const char * get_device_id(LPCGUID pGuid)
218 {
219     if (IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
220         return "DSDEVID_DefaultPlayback";
221     else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
222         return "DSDEVID_DefaultVoicePlayback";
223     else if (IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
224         return "DSDEVID_DefaultCapture";
225     else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
226         return "DSDEVID_DefaultVoiceCapture";
227     return debugstr_guid(pGuid);
228 }
229
230 /***************************************************************************
231  * GetDeviceID  [DSOUND.9]
232  *
233  * Retrieves unique identifier of default device specified
234  *
235  * PARAMS
236  *    pGuidSrc  [I] Address of device GUID.
237  *    pGuidDest [O] Address to receive unique device GUID.
238  *
239  * RETURNS
240  *    Success: DS_OK
241  *    Failure: DSERR_INVALIDPARAM
242  *
243  * NOTES
244  *    pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
245  *    DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
246  *    DSDEVID_DefaultVoiceCapture.
247  *    Returns pGuidSrc if pGuidSrc is a valid device or the device
248  *    GUID for the specified constants.
249  */
250 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
251 {
252     TRACE("(%s,%p)\n", get_device_id(pGuidSrc),pGuidDest);
253
254     if ( pGuidSrc == NULL) {
255         WARN("invalid parameter: pGuidSrc == NULL\n");
256         return DSERR_INVALIDPARAM;
257     }
258
259     if ( pGuidDest == NULL ) {
260         WARN("invalid parameter: pGuidDest == NULL\n");
261         return DSERR_INVALIDPARAM;
262     }
263
264     if ( IsEqualGUID( &DSDEVID_DefaultPlayback, pGuidSrc ) ||
265          IsEqualGUID( &DSDEVID_DefaultVoicePlayback, pGuidSrc ) ) {
266         CopyMemory(pGuidDest, &DSOUND_renderer_guids[ds_default_playback], sizeof(GUID));
267         TRACE("returns %s\n", get_device_id(pGuidDest));
268         return DS_OK;
269     }
270
271     if ( IsEqualGUID( &DSDEVID_DefaultCapture, pGuidSrc ) ||
272          IsEqualGUID( &DSDEVID_DefaultVoiceCapture, pGuidSrc ) ) {
273         CopyMemory(pGuidDest, &DSOUND_capture_guids[ds_default_capture], sizeof(GUID));
274         TRACE("returns %s\n", get_device_id(pGuidDest));
275         return DS_OK;
276     }
277
278     CopyMemory(pGuidDest, pGuidSrc, sizeof(GUID));
279     TRACE("returns %s\n", get_device_id(pGuidDest));
280
281     return DS_OK;
282 }
283
284
285 /***************************************************************************
286  * DirectSoundEnumerateA [DSOUND.2]
287  *
288  * Enumerate all DirectSound drivers installed in the system
289  *
290  * PARAMS
291  *    lpDSEnumCallback  [I] Address of callback function.
292  *    lpContext         [I] Address of user defined context passed to callback function.
293  *
294  * RETURNS
295  *    Success: DS_OK
296  *    Failure: DSERR_INVALIDPARAM
297  */
298 HRESULT WINAPI DirectSoundEnumerateA(
299     LPDSENUMCALLBACKA lpDSEnumCallback,
300     LPVOID lpContext)
301 {
302     unsigned devs, wod;
303     DSDRIVERDESC desc;
304     GUID guid;
305     int err;
306
307     TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
308         lpDSEnumCallback, lpContext);
309
310     if (lpDSEnumCallback == NULL) {
311         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
312         return DSERR_INVALIDPARAM;
313     }
314
315     devs = waveOutGetNumDevs();
316     if (devs > 0) {
317         if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
318             for (wod = 0; wod < devs; ++wod) {
319                 if (IsEqualGUID( &guid, &DSOUND_renderer_guids[wod]) ) {
320                     err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
321                     if (err == DS_OK) {
322                         TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
323                               "Primary Sound Driver",desc.szDrvname,lpContext);
324                         if (lpDSEnumCallback(NULL, "Primary Sound Driver", desc.szDrvname, lpContext) == FALSE)
325                             return DS_OK;
326                     }
327                 }
328             }
329         }
330     }
331
332     for (wod = 0; wod < devs; ++wod) {
333         err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
334         if (err == DS_OK) {
335             TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
336                   debugstr_guid(&DSOUND_renderer_guids[wod]),desc.szDesc,desc.szDrvname,lpContext);
337             if (lpDSEnumCallback(&DSOUND_renderer_guids[wod], desc.szDesc, desc.szDrvname, lpContext) == FALSE)
338                 return DS_OK;
339         }
340     }
341     return DS_OK;
342 }
343
344 /***************************************************************************
345  * DirectSoundEnumerateW [DSOUND.3]
346  *
347  * Enumerate all DirectSound drivers installed in the system
348  *
349  * PARAMS
350  *    lpDSEnumCallback  [I] Address of callback function.
351  *    lpContext         [I] Address of user defined context passed to callback function.
352  *
353  * RETURNS
354  *    Success: DS_OK
355  *    Failure: DSERR_INVALIDPARAM
356  */
357 HRESULT WINAPI DirectSoundEnumerateW(
358         LPDSENUMCALLBACKW lpDSEnumCallback,
359         LPVOID lpContext )
360 {
361     unsigned devs, wod;
362     DSDRIVERDESC desc;
363     GUID guid;
364     int err;
365     WCHAR wDesc[MAXPNAMELEN];
366     WCHAR wName[MAXPNAMELEN];
367
368     TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
369         lpDSEnumCallback, lpContext);
370
371     if (lpDSEnumCallback == NULL) {
372         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
373         return DSERR_INVALIDPARAM;
374     }
375
376     devs = waveOutGetNumDevs();
377     if (devs > 0) {
378         if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
379             for (wod = 0; wod < devs; ++wod) {
380                 if (IsEqualGUID( &guid, &DSOUND_renderer_guids[wod] ) ) {
381                     err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
382                     if (err == DS_OK) {
383                         TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
384                               "Primary Sound Driver",desc.szDrvname,lpContext);
385                         MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
386                                              wDesc, sizeof(wDesc)/sizeof(WCHAR) );
387                         MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
388                                              wName, sizeof(wName)/sizeof(WCHAR) );
389                         if (lpDSEnumCallback(NULL, wDesc, wName, lpContext) == FALSE)
390                             return DS_OK;
391                     }
392                 }
393             }
394         }
395     }
396
397     for (wod = 0; wod < devs; ++wod) {
398         err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
399         if (err == DS_OK) {
400             TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
401                   debugstr_guid(&DSOUND_renderer_guids[wod]),desc.szDesc,desc.szDrvname,lpContext);
402             MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
403                                  wDesc, sizeof(wDesc)/sizeof(WCHAR) );
404             MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
405                                  wName, sizeof(wName)/sizeof(WCHAR) );
406             if (lpDSEnumCallback(&DSOUND_renderer_guids[wod], wDesc, wName, lpContext) == FALSE)
407                 return DS_OK;
408         }
409     }
410     return DS_OK;
411 }
412
413 /*******************************************************************************
414  * DirectSound ClassFactory
415  */
416
417 static HRESULT WINAPI
418 DSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
419         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
420
421         FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
422         return E_NOINTERFACE;
423 }
424
425 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
426 {
427     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
428     ULONG ref = InterlockedIncrement(&(This->ref));
429     TRACE("(%p) ref was %ld\n", This, ref - 1);
430     return ref;
431 }
432
433 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
434 {
435     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
436     ULONG ref = InterlockedDecrement(&(This->ref));
437     TRACE("(%p) ref was %ld\n", This, ref + 1);
438     /* static class, won't be freed */
439     return ref;
440 }
441
442 static HRESULT WINAPI DSCF_CreateInstance(
443         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
444 ) {
445         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
446         TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
447
448         if (pOuter)
449                 return CLASS_E_NOAGGREGATION;
450
451         if (ppobj == NULL) {
452                 WARN("invalid parameter\n");
453                 return DSERR_INVALIDPARAM;
454         }
455
456         *ppobj = NULL;
457
458         if ( IsEqualIID( &IID_IDirectSound, riid ) )
459                 return DSOUND_Create((LPDIRECTSOUND*)ppobj,pOuter);
460
461         if ( IsEqualIID( &IID_IDirectSound8, riid ) )
462                 return DSOUND_Create8((LPDIRECTSOUND8*)ppobj,pOuter);
463
464         WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);     
465         return E_NOINTERFACE;
466 }
467
468 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
469         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
470         FIXME("(%p)->(%d),stub!\n",This,dolock);
471         return S_OK;
472 }
473
474 static const IClassFactoryVtbl DSCF_Vtbl = {
475         DSCF_QueryInterface,
476         DSCF_AddRef,
477         DSCF_Release,
478         DSCF_CreateInstance,
479         DSCF_LockServer
480 };
481
482 static IClassFactoryImpl DSOUND_CF = { &DSCF_Vtbl, 1 };
483
484 /*******************************************************************************
485  * DirectSoundPrivate ClassFactory
486  */
487
488 static HRESULT WINAPI
489 DSPCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
490         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
491
492         FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
493         return E_NOINTERFACE;
494 }
495
496 static ULONG WINAPI DSPCF_AddRef(LPCLASSFACTORY iface)
497 {
498     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
499     ULONG ref = InterlockedIncrement(&(This->ref));
500     TRACE("(%p) ref was %ld\n", This, ref - 1);
501     return ref;
502 }
503
504 static ULONG WINAPI DSPCF_Release(LPCLASSFACTORY iface)
505 {
506     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
507     ULONG ref = InterlockedDecrement(&(This->ref));
508     TRACE("(%p) ref was %ld\n", This, ref + 1);
509     /* static class, won't be freed */
510     return ref;
511 }
512
513 static HRESULT WINAPI
514 DSPCF_CreateInstance(
515         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
516 ) {
517         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
518         TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
519
520         if (ppobj == NULL) {
521                 WARN("invalid parameter\n");
522                 return DSERR_INVALIDPARAM;
523         }
524
525         *ppobj = NULL;
526
527         if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
528                 return IKsPrivatePropertySetImpl_Create((IKsPrivatePropertySetImpl**)ppobj);
529         }
530
531         WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);     
532         return E_NOINTERFACE;
533 }
534
535 static HRESULT WINAPI
536 DSPCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
537         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
538         FIXME("(%p)->(%d),stub!\n",This,dolock);
539         return S_OK;
540 }
541
542 static const IClassFactoryVtbl DSPCF_Vtbl = {
543         DSPCF_QueryInterface,
544         DSPCF_AddRef,
545         DSPCF_Release,
546         DSPCF_CreateInstance,
547         DSPCF_LockServer
548 };
549
550 static IClassFactoryImpl DSOUND_PRIVATE_CF = { &DSPCF_Vtbl, 1 };
551
552 /*******************************************************************************
553  * DllGetClassObject [DSOUND.@]
554  * Retrieves class object from a DLL object
555  *
556  * NOTES
557  *    Docs say returns STDAPI
558  *
559  * PARAMS
560  *    rclsid [I] CLSID for the class object
561  *    riid   [I] Reference to identifier of interface for class object
562  *    ppv    [O] Address of variable to receive interface pointer for riid
563  *
564  * RETURNS
565  *    Success: S_OK
566  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
567  *             E_UNEXPECTED
568  */
569 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
570 {
571     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
572
573     if (ppv == NULL) {
574         WARN("invalid parameter\n");
575         return E_INVALIDARG;
576     }
577
578     *ppv = NULL;
579
580     if ( IsEqualCLSID( &CLSID_DirectSound, rclsid ) ||
581          IsEqualCLSID( &CLSID_DirectSound8, rclsid ) ) {
582         if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
583             *ppv = (LPVOID)&DSOUND_CF;
584             IClassFactory_AddRef((IClassFactory*)*ppv);
585             return S_OK;
586         }
587         WARN("(%s,%s,%p): no interface found.\n",
588             debugstr_guid(rclsid), debugstr_guid(riid), ppv);
589         return S_FALSE;
590     }
591
592     if ( IsEqualCLSID( &CLSID_DirectSoundCapture, rclsid ) ||
593          IsEqualCLSID( &CLSID_DirectSoundCapture8, rclsid ) ) {
594         if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
595             *ppv = (LPVOID)&DSOUND_CAPTURE_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_DirectSoundFullDuplex, rclsid ) ) {
605         if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
606             *ppv = (LPVOID)&DSOUND_FULLDUPLEX_CF;
607             IClassFactory_AddRef((IClassFactory*)*ppv);
608             return S_OK;
609         }
610         WARN("(%s,%s,%p): no interface found.\n",
611             debugstr_guid(rclsid), debugstr_guid(riid), ppv);
612         return S_FALSE;
613     }
614
615     if ( IsEqualCLSID( &CLSID_DirectSoundPrivate, rclsid ) ) {
616         if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
617             *ppv = (LPVOID)&DSOUND_PRIVATE_CF;
618             IClassFactory_AddRef((IClassFactory*)*ppv);
619             return S_OK;
620         }
621         WARN("(%s,%s,%p): no interface found.\n",
622             debugstr_guid(rclsid), debugstr_guid(riid), ppv);
623         return S_FALSE;
624     }
625
626     WARN("(%s,%s,%p): no class found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
627     return CLASS_E_CLASSNOTAVAILABLE;
628 }
629
630
631 /*******************************************************************************
632  * DllCanUnloadNow [DSOUND.4]
633  * Determines whether the DLL is in use.
634  *
635  * RETURNS
636  *    Success: S_OK
637  *    Failure: S_FALSE
638  */
639 HRESULT WINAPI DllCanUnloadNow(void)
640 {
641     FIXME("(void): stub\n");
642     return S_FALSE;
643 }
644
645 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)      \
646         guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2;               \
647         guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3;     \
648         guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6;     \
649         guid.Data4[6] = b7; guid.Data4[7] = b8;
650
651 /***********************************************************************
652  *           DllMain (DSOUND.init)
653  */
654 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
655 {
656     int i;
657     TRACE("(%p %ld %p)\n", hInstDLL, fdwReason, lpvReserved);
658
659     switch (fdwReason) {
660     case DLL_PROCESS_ATTACH:
661         TRACE("DLL_PROCESS_ATTACH\n");
662         for (i = 0; i < MAXWAVEDRIVERS; i++) {
663             DSOUND_renderer[i] = NULL;
664             DSOUND_capture[i] = NULL;
665             INIT_GUID(DSOUND_renderer_guids[i], 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
666             INIT_GUID(DSOUND_capture_guids[i],  0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
667         }
668         break;
669     case DLL_PROCESS_DETACH:
670         TRACE("DLL_PROCESS_DETACH\n");
671         break;
672     case DLL_THREAD_ATTACH:
673         TRACE("DLL_THREAD_ATTACH\n");
674         break;
675     case DLL_THREAD_DETACH:
676         TRACE("DLL_THREAD_DETACH\n");
677         break;
678     default:
679         TRACE("UNKNOWN REASON\n");
680         break;
681     }
682     return TRUE;
683 }