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