mscms: Added missing breaks (Coverity).
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * Most thread locking is complete. There may be a few race
22  * conditions still lurking.
23  *
24  * TODO:
25  *      Implement SetCooperativeLevel properly (need to address focus issues)
26  *      Implement DirectSound3DBuffers (stubs in place)
27  *      Use hardware 3D support if available
28  *      Add critical section locking inside Release and AddRef methods
29  *      Handle static buffers - put those in hardware, non-static not in hardware
30  *      Hardware DuplicateSoundBuffer
31  *      Proper volume calculation for 3d buffers
32  *      Remove DS_HEL_FRAGS and use mixer fragment length for it
33  */
34
35 #include <stdarg.h>
36
37 #define COBJMACROS
38 #define NONAMELESSSTRUCT
39 #define NONAMELESSUNION
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "winreg.h"
45 #include "mmsystem.h"
46 #include "winternl.h"
47 #include "mmddk.h"
48 #include "wine/debug.h"
49 #include "dsound.h"
50 #include "dsconf.h"
51 #include "ks.h"
52 #include "rpcproxy.h"
53 #include "initguid.h"
54 #include "ksmedia.h"
55 #include "dsdriver.h"
56
57 #include "dsound_private.h"
58
59 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
60
61 DirectSoundDevice*      DSOUND_renderer[MAXWAVEDRIVERS];
62 GUID                    DSOUND_renderer_guids[MAXWAVEDRIVERS];
63 GUID                    DSOUND_capture_guids[MAXWAVEDRIVERS];
64
65 HRESULT mmErr(UINT err)
66 {
67         switch(err) {
68         case MMSYSERR_NOERROR:
69                 return DS_OK;
70         case MMSYSERR_ALLOCATED:
71                 return DSERR_ALLOCATED;
72         case MMSYSERR_ERROR:
73         case MMSYSERR_INVALHANDLE:
74         case WAVERR_STILLPLAYING:
75                 return DSERR_GENERIC; /* FIXME */
76         case MMSYSERR_NODRIVER:
77                 return DSERR_NODRIVER;
78         case MMSYSERR_NOMEM:
79                 return DSERR_OUTOFMEMORY;
80         case MMSYSERR_INVALPARAM:
81         case WAVERR_BADFORMAT:
82         case WAVERR_UNPREPARED:
83                 return DSERR_INVALIDPARAM;
84         case MMSYSERR_NOTSUPPORTED:
85                 return DSERR_UNSUPPORTED;
86         default:
87                 FIXME("Unknown MMSYS error %d\n",err);
88                 return DSERR_GENERIC;
89         }
90 }
91
92 /* All default settings, you most likely don't want to touch these, see wiki on UsefulRegistryKeys */
93 int ds_emuldriver = 0;
94 int ds_hel_buflen = 32768 * 2;
95 int ds_snd_queue_max = 10;
96 int ds_snd_queue_min = 6;
97 int ds_snd_shadow_maxsize = 2;
98 int ds_hw_accel = DS_HW_ACCEL_FULL;
99 int ds_default_sample_rate = 44100;
100 int ds_default_bits_per_sample = 16;
101 static int ds_default_playback;
102 static int ds_default_capture;
103 static HINSTANCE instance;
104
105 /*
106  * Get a config key from either the app-specific or the default config
107  */
108
109 static inline DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
110                                     char *buffer, DWORD size )
111 {
112     if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
113     if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
114     return ERROR_FILE_NOT_FOUND;
115 }
116
117
118 /*
119  * Setup the dsound options.
120  */
121
122 void setup_dsound_options(void)
123 {
124     char buffer[MAX_PATH+16];
125     HKEY hkey, appkey = 0;
126     DWORD len;
127
128     buffer[MAX_PATH]='\0';
129
130     /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
131     if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectSound", &hkey )) hkey = 0;
132
133     len = GetModuleFileNameA( 0, buffer, MAX_PATH );
134     if (len && len < MAX_PATH)
135     {
136         HKEY tmpkey;
137         /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
138         if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
139         {
140             char *p, *appname = buffer;
141             if ((p = strrchr( appname, '/' ))) appname = p + 1;
142             if ((p = strrchr( appname, '\\' ))) appname = p + 1;
143             strcat( appname, "\\DirectSound" );
144             TRACE("appname = [%s]\n", appname);
145             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
146             RegCloseKey( tmpkey );
147         }
148     }
149
150     /* get options */
151
152     if (!get_config_key( hkey, appkey, "EmulDriver", buffer, MAX_PATH ))
153         ds_emuldriver = strcmp(buffer, "N");
154
155     if (!get_config_key( hkey, appkey, "HelBuflen", buffer, MAX_PATH ))
156         ds_hel_buflen = atoi(buffer);
157
158     if (!get_config_key( hkey, appkey, "SndQueueMax", buffer, MAX_PATH ))
159         ds_snd_queue_max = atoi(buffer);
160
161     if (!get_config_key( hkey, appkey, "SndQueueMin", buffer, MAX_PATH ))
162         ds_snd_queue_min = atoi(buffer);
163
164     if (!get_config_key( hkey, appkey, "HardwareAcceleration", buffer, MAX_PATH )) {
165         if (strcmp(buffer, "Full") == 0)
166             ds_hw_accel = DS_HW_ACCEL_FULL;
167         else if (strcmp(buffer, "Standard") == 0)
168             ds_hw_accel = DS_HW_ACCEL_STANDARD;
169         else if (strcmp(buffer, "Basic") == 0)
170             ds_hw_accel = DS_HW_ACCEL_BASIC;
171         else if (strcmp(buffer, "Emulation") == 0)
172             ds_hw_accel = DS_HW_ACCEL_EMULATION;
173     }
174
175     if (!get_config_key( hkey, appkey, "DefaultPlayback", buffer, MAX_PATH ))
176         ds_default_playback = atoi(buffer);
177
178     if (!get_config_key( hkey, appkey, "MaxShadowSize", buffer, MAX_PATH ))
179         ds_snd_shadow_maxsize = atoi(buffer);
180
181     if (!get_config_key( hkey, appkey, "DefaultCapture", buffer, MAX_PATH ))
182         ds_default_capture = atoi(buffer);
183
184     if (!get_config_key( hkey, appkey, "DefaultSampleRate", buffer, MAX_PATH ))
185         ds_default_sample_rate = atoi(buffer);
186
187     if (!get_config_key( hkey, appkey, "DefaultBitsPerSample", buffer, MAX_PATH ))
188         ds_default_bits_per_sample = atoi(buffer);
189
190     if (appkey) RegCloseKey( appkey );
191     if (hkey) RegCloseKey( hkey );
192
193     TRACE("ds_emuldriver = %d\n", ds_emuldriver);
194     TRACE("ds_hel_buflen = %d\n", ds_hel_buflen);
195     TRACE("ds_snd_queue_max = %d\n", ds_snd_queue_max);
196     TRACE("ds_snd_queue_min = %d\n", ds_snd_queue_min);
197     TRACE("ds_hw_accel = %s\n",
198         ds_hw_accel==DS_HW_ACCEL_FULL ? "Full" :
199         ds_hw_accel==DS_HW_ACCEL_STANDARD ? "Standard" :
200         ds_hw_accel==DS_HW_ACCEL_BASIC ? "Basic" :
201         ds_hw_accel==DS_HW_ACCEL_EMULATION ? "Emulation" :
202         "Unknown");
203     TRACE("ds_default_playback = %d\n", ds_default_playback);
204     TRACE("ds_default_capture = %d\n", ds_default_playback);
205     TRACE("ds_default_sample_rate = %d\n", ds_default_sample_rate);
206     TRACE("ds_default_bits_per_sample = %d\n", ds_default_bits_per_sample);
207     TRACE("ds_snd_shadow_maxsize = %d\n", ds_snd_shadow_maxsize);
208 }
209
210 static const char * get_device_id(LPCGUID pGuid)
211 {
212     if (IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
213         return "DSDEVID_DefaultPlayback";
214     else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
215         return "DSDEVID_DefaultVoicePlayback";
216     else if (IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
217         return "DSDEVID_DefaultCapture";
218     else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
219         return "DSDEVID_DefaultVoiceCapture";
220     return debugstr_guid(pGuid);
221 }
222
223 /***************************************************************************
224  * GetDeviceID  [DSOUND.9]
225  *
226  * Retrieves unique identifier of default device specified
227  *
228  * PARAMS
229  *    pGuidSrc  [I] Address of device GUID.
230  *    pGuidDest [O] Address to receive unique device GUID.
231  *
232  * RETURNS
233  *    Success: DS_OK
234  *    Failure: DSERR_INVALIDPARAM
235  *
236  * NOTES
237  *    pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
238  *    DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
239  *    DSDEVID_DefaultVoiceCapture.
240  *    Returns pGuidSrc if pGuidSrc is a valid device or the device
241  *    GUID for the specified constants.
242  */
243 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
244 {
245     TRACE("(%s,%p)\n", get_device_id(pGuidSrc),pGuidDest);
246
247     if ( pGuidSrc == NULL) {
248         WARN("invalid parameter: pGuidSrc == NULL\n");
249         return DSERR_INVALIDPARAM;
250     }
251
252     if ( pGuidDest == NULL ) {
253         WARN("invalid parameter: pGuidDest == NULL\n");
254         return DSERR_INVALIDPARAM;
255     }
256
257     if ( IsEqualGUID( &DSDEVID_DefaultPlayback, pGuidSrc ) ||
258          IsEqualGUID( &DSDEVID_DefaultVoicePlayback, pGuidSrc ) ) {
259         *pGuidDest = DSOUND_renderer_guids[ds_default_playback];
260         TRACE("returns %s\n", get_device_id(pGuidDest));
261         return DS_OK;
262     }
263
264     if ( IsEqualGUID( &DSDEVID_DefaultCapture, pGuidSrc ) ||
265          IsEqualGUID( &DSDEVID_DefaultVoiceCapture, pGuidSrc ) ) {
266         *pGuidDest = DSOUND_capture_guids[ds_default_capture];
267         TRACE("returns %s\n", get_device_id(pGuidDest));
268         return DS_OK;
269     }
270
271     *pGuidDest = *pGuidSrc;
272     TRACE("returns %s\n", get_device_id(pGuidDest));
273
274     return DS_OK;
275 }
276
277 struct morecontext
278 {
279     LPDSENUMCALLBACKA callA;
280     LPVOID data;
281 };
282
283 static BOOL CALLBACK a_to_w_callback(LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
284 {
285     struct morecontext *context = data;
286     char descA[MAXPNAMELEN], modA[MAXPNAMELEN];
287
288     WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, sizeof(descA), NULL, NULL);
289     WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, sizeof(modA), NULL, NULL);
290
291     return context->callA(guid, descA, modA, context->data);
292 }
293
294 /***************************************************************************
295  * DirectSoundEnumerateA [DSOUND.2]
296  *
297  * Enumerate all DirectSound drivers installed in the system
298  *
299  * PARAMS
300  *    lpDSEnumCallback  [I] Address of callback function.
301  *    lpContext         [I] Address of user defined context passed to callback function.
302  *
303  * RETURNS
304  *    Success: DS_OK
305  *    Failure: DSERR_INVALIDPARAM
306  */
307 HRESULT WINAPI DirectSoundEnumerateA(
308     LPDSENUMCALLBACKA lpDSEnumCallback,
309     LPVOID lpContext)
310 {
311     struct morecontext context;
312
313     if (lpDSEnumCallback == NULL) {
314         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
315         return DSERR_INVALIDPARAM;
316     }
317
318     context.callA = lpDSEnumCallback;
319     context.data = lpContext;
320
321     return DirectSoundEnumerateW(a_to_w_callback, &context);
322 }
323
324 /***************************************************************************
325  * DirectSoundEnumerateW [DSOUND.3]
326  *
327  * Enumerate all DirectSound drivers installed in the system
328  *
329  * PARAMS
330  *    lpDSEnumCallback  [I] Address of callback function.
331  *    lpContext         [I] Address of user defined context passed to callback function.
332  *
333  * RETURNS
334  *    Success: DS_OK
335  *    Failure: DSERR_INVALIDPARAM
336  */
337 HRESULT WINAPI DirectSoundEnumerateW(
338         LPDSENUMCALLBACKW lpDSEnumCallback,
339         LPVOID lpContext )
340 {
341     unsigned devs, wod;
342     DSDRIVERDESC desc;
343     GUID guid;
344     int err;
345     WCHAR wDesc[MAXPNAMELEN];
346     WCHAR wName[MAXPNAMELEN];
347
348     TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
349         lpDSEnumCallback, lpContext);
350
351     if (lpDSEnumCallback == NULL) {
352         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
353         return DSERR_INVALIDPARAM;
354     }
355
356     devs = waveOutGetNumDevs();
357     if (devs > 0) {
358         if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
359             static const WCHAR empty[] = { 0 };
360             for (wod = 0; wod < devs; ++wod) {
361                 if (IsEqualGUID( &guid, &DSOUND_renderer_guids[wod] ) ) {
362                     err = mmErr(waveOutMessage(UlongToHandle(wod),DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
363                     if (err == DS_OK) {
364                         TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
365                               "Primary Sound Driver",desc.szDrvname,lpContext);
366                         MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
367                                              wDesc, sizeof(wDesc)/sizeof(WCHAR) );
368                         if (lpDSEnumCallback(NULL, wDesc, empty, lpContext) == FALSE)
369                             return DS_OK;
370                     }
371                 }
372             }
373         }
374     }
375
376     for (wod = 0; wod < devs; ++wod) {
377         err = mmErr(waveOutMessage(UlongToHandle(wod),DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
378         if (err == DS_OK) {
379             TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
380                   debugstr_guid(&DSOUND_renderer_guids[wod]),desc.szDesc,desc.szDrvname,lpContext);
381             MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
382                                  wDesc, sizeof(wDesc)/sizeof(WCHAR) );
383             wDesc[(sizeof(wDesc)/sizeof(WCHAR)) - 1] = '\0';
384
385             MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
386                                  wName, sizeof(wName)/sizeof(WCHAR) );
387             wName[(sizeof(wName)/sizeof(WCHAR)) - 1] = '\0';
388
389             if (lpDSEnumCallback(&DSOUND_renderer_guids[wod], wDesc, wName, lpContext) == FALSE)
390                 return DS_OK;
391         }
392     }
393     return DS_OK;
394 }
395
396 /***************************************************************************
397  * DirectSoundCaptureEnumerateA [DSOUND.7]
398  *
399  * Enumerate all DirectSound drivers installed in the system.
400  *
401  * PARAMS
402  *    lpDSEnumCallback  [I] Address of callback function.
403  *    lpContext         [I] Address of user defined context passed to callback function.
404  *
405  * RETURNS
406  *    Success: DS_OK
407  *    Failure: DSERR_INVALIDPARAM
408  */
409 HRESULT WINAPI DirectSoundCaptureEnumerateA(
410     LPDSENUMCALLBACKA lpDSEnumCallback,
411     LPVOID lpContext)
412 {
413     struct morecontext context;
414
415     if (lpDSEnumCallback == NULL) {
416         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
417         return DSERR_INVALIDPARAM;
418     }
419
420     context.callA = lpDSEnumCallback;
421     context.data = lpContext;
422
423     return DirectSoundCaptureEnumerateW(a_to_w_callback, &context);
424 }
425
426 /***************************************************************************
427  * DirectSoundCaptureEnumerateW [DSOUND.8]
428  *
429  * Enumerate all DirectSound drivers installed in the system.
430  *
431  * PARAMS
432  *    lpDSEnumCallback  [I] Address of callback function.
433  *    lpContext         [I] Address of user defined context passed to callback function.
434  *
435  * RETURNS
436  *    Success: DS_OK
437  *    Failure: DSERR_INVALIDPARAM
438  */
439 HRESULT WINAPI
440 DirectSoundCaptureEnumerateW(
441     LPDSENUMCALLBACKW lpDSEnumCallback,
442     LPVOID lpContext)
443 {
444     unsigned devs, wid;
445     DSDRIVERDESC desc;
446     GUID guid;
447     int err;
448     WCHAR wDesc[MAXPNAMELEN];
449     WCHAR wName[MAXPNAMELEN];
450
451     TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
452
453     if (lpDSEnumCallback == NULL) {
454         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
455         return DSERR_INVALIDPARAM;
456     }
457
458     devs = waveInGetNumDevs();
459     if (devs > 0) {
460         if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
461             for (wid = 0; wid < devs; ++wid) {
462                 if (IsEqualGUID( &guid, &DSOUND_capture_guids[wid] ) ) {
463                     err = mmErr(waveInMessage(UlongToHandle(wid),DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
464                     if (err == DS_OK) {
465                         TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
466                               "Primary Sound Capture Driver",desc.szDrvname,lpContext);
467                         MultiByteToWideChar( CP_ACP, 0, "Primary Sound Capture Driver", -1,
468                                              wDesc, sizeof(wDesc)/sizeof(WCHAR) );
469                         MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
470                                              wName, sizeof(wName)/sizeof(WCHAR) );
471                         wName[(sizeof(wName)/sizeof(WCHAR)) - 1] = '\0';
472
473                         if (lpDSEnumCallback(NULL, wDesc, wName, lpContext) == FALSE)
474                             return DS_OK;
475                     }
476                 }
477             }
478         }
479     }
480
481     for (wid = 0; wid < devs; ++wid) {
482         err = mmErr(waveInMessage(UlongToHandle(wid),DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
483         if (err == DS_OK) {
484             TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
485                   debugstr_guid(&DSOUND_capture_guids[wid]),desc.szDesc,desc.szDrvname,lpContext);
486             MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
487                                  wDesc, sizeof(wDesc)/sizeof(WCHAR) );
488             wDesc[(sizeof(wDesc)/sizeof(WCHAR)) - 1] = '\0';
489
490             MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
491                                  wName, sizeof(wName)/sizeof(WCHAR) );
492             wName[(sizeof(wName)/sizeof(WCHAR)) - 1] = '\0';
493
494             if (lpDSEnumCallback(&DSOUND_capture_guids[wid], wDesc, wName, lpContext) == FALSE)
495                 return DS_OK;
496         }
497     }
498
499     return DS_OK;
500 }
501
502 /*******************************************************************************
503  * DirectSound ClassFactory
504  */
505
506 typedef  HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
507  
508 typedef struct {
509     IClassFactory IClassFactory_iface;
510     REFCLSID rclsid;
511     FnCreateInstance pfnCreateInstance;
512 } IClassFactoryImpl;
513
514 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
515 {
516     return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
517 }
518
519 static HRESULT WINAPI
520 DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
521 {
522     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
523     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
524     if (ppobj == NULL)
525         return E_POINTER;
526     if (IsEqualIID(riid, &IID_IUnknown) ||
527         IsEqualIID(riid, &IID_IClassFactory))
528     {
529         *ppobj = iface;
530         IUnknown_AddRef(iface);
531         return S_OK;
532     }
533     *ppobj = NULL;
534     return E_NOINTERFACE;
535 }
536
537 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
538 {
539     return 2;
540 }
541
542 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
543 {
544     /* static class, won't be freed */
545     return 1;
546 }
547
548 static HRESULT WINAPI DSCF_CreateInstance(
549     LPCLASSFACTORY iface,
550     LPUNKNOWN pOuter,
551     REFIID riid,
552     LPVOID *ppobj)
553 {
554     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
555     TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
556
557     if (pOuter)
558         return CLASS_E_NOAGGREGATION;
559
560     if (ppobj == NULL) {
561         WARN("invalid parameter\n");
562         return DSERR_INVALIDPARAM;
563     }
564     *ppobj = NULL;
565     return This->pfnCreateInstance(riid, ppobj);
566 }
567  
568 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
569 {
570     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
571     FIXME("(%p, %d) stub!\n", This, dolock);
572     return S_OK;
573 }
574
575 static const IClassFactoryVtbl DSCF_Vtbl = {
576     DSCF_QueryInterface,
577     DSCF_AddRef,
578     DSCF_Release,
579     DSCF_CreateInstance,
580     DSCF_LockServer
581 };
582
583 static IClassFactoryImpl DSOUND_CF[] = {
584     { { &DSCF_Vtbl }, &CLSID_DirectSound, (FnCreateInstance)DSOUND_Create },
585     { { &DSCF_Vtbl }, &CLSID_DirectSound8, (FnCreateInstance)DSOUND_Create8 },
586     { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture, (FnCreateInstance)DSOUND_CaptureCreate },
587     { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture8, (FnCreateInstance)DSOUND_CaptureCreate8 },
588     { { &DSCF_Vtbl }, &CLSID_DirectSoundFullDuplex, (FnCreateInstance)DSOUND_FullDuplexCreate },
589     { { &DSCF_Vtbl }, &CLSID_DirectSoundPrivate, (FnCreateInstance)IKsPrivatePropertySetImpl_Create },
590     { { NULL }, NULL, NULL }
591 };
592
593 /*******************************************************************************
594  * DllGetClassObject [DSOUND.@]
595  * Retrieves class object from a DLL object
596  *
597  * NOTES
598  *    Docs say returns STDAPI
599  *
600  * PARAMS
601  *    rclsid [I] CLSID for the class object
602  *    riid   [I] Reference to identifier of interface for class object
603  *    ppv    [O] Address of variable to receive interface pointer for riid
604  *
605  * RETURNS
606  *    Success: S_OK
607  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
608  *             E_UNEXPECTED
609  */
610 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
611 {
612     int i = 0;
613     TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
614
615     if (ppv == NULL) {
616         WARN("invalid parameter\n");
617         return E_INVALIDARG;
618     }
619
620     *ppv = NULL;
621
622     if (!IsEqualIID(riid, &IID_IClassFactory) &&
623         !IsEqualIID(riid, &IID_IUnknown)) {
624         WARN("no interface for %s\n", debugstr_guid(riid));
625         return E_NOINTERFACE;
626     }
627
628     while (NULL != DSOUND_CF[i].rclsid) {
629         if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
630             DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
631             *ppv = &DSOUND_CF[i];
632             return S_OK;
633         }
634         i++;
635     }
636
637     WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
638          debugstr_guid(riid), ppv);
639     return CLASS_E_CLASSNOTAVAILABLE;
640 }
641
642
643 /*******************************************************************************
644  * DllCanUnloadNow [DSOUND.4]
645  * Determines whether the DLL is in use.
646  *
647  * RETURNS
648  *    Can unload now: S_OK
649  *    Cannot unload now (the DLL is still active): S_FALSE
650  */
651 HRESULT WINAPI DllCanUnloadNow(void)
652 {
653     return S_FALSE;
654 }
655
656 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)      \
657         guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2;               \
658         guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3;     \
659         guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6;     \
660         guid.Data4[6] = b7; guid.Data4[7] = b8;
661
662 /***********************************************************************
663  *           DllMain (DSOUND.init)
664  */
665 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
666 {
667     int i;
668     TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpvReserved);
669
670     switch (fdwReason) {
671     case DLL_PROCESS_ATTACH:
672         TRACE("DLL_PROCESS_ATTACH\n");
673         for (i = 0; i < MAXWAVEDRIVERS; i++) {
674             DSOUND_renderer[i] = NULL;
675             DSOUND_capture[i] = NULL;
676             INIT_GUID(DSOUND_renderer_guids[i], 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
677             INIT_GUID(DSOUND_capture_guids[i],  0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
678         }
679         instance = hInstDLL;
680         DisableThreadLibraryCalls(hInstDLL);
681         /* Increase refcount on dsound by 1 */
682         GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
683         break;
684     case DLL_PROCESS_DETACH:
685         TRACE("DLL_PROCESS_DETACH\n");
686         break;
687     default:
688         TRACE("UNKNOWN REASON\n");
689         break;
690     }
691     return TRUE;
692 }
693
694 /***********************************************************************
695  *              DllRegisterServer (DSOUND.@)
696  */
697 HRESULT WINAPI DllRegisterServer(void)
698 {
699     return __wine_register_resources( instance, NULL );
700 }
701
702 /***********************************************************************
703  *              DllUnregisterServer (DSOUND.@)
704  */
705 HRESULT WINAPI DllUnregisterServer(void)
706 {
707     return __wine_unregister_resources( instance, NULL );
708 }