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