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