vbscript: Added interp_gteq implementation.
[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     setup_dsound_options();
357
358     devs = waveOutGetNumDevs();
359     if (devs > 0) {
360         if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
361             static const WCHAR empty[] = { 0 };
362             for (wod = 0; wod < devs; ++wod) {
363                 if (IsEqualGUID( &guid, &DSOUND_renderer_guids[wod] ) ) {
364                     err = mmErr(waveOutMessage(UlongToHandle(wod),DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,ds_hw_accel));
365                     if (err == DS_OK) {
366                         TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
367                               "Primary Sound Driver",desc.szDrvname,lpContext);
368                         MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
369                                              wDesc, sizeof(wDesc)/sizeof(WCHAR) );
370                         if (lpDSEnumCallback(NULL, wDesc, empty, lpContext) == FALSE)
371                             return DS_OK;
372                     }
373                 }
374             }
375         }
376     }
377
378     for (wod = 0; wod < devs; ++wod) {
379         err = mmErr(waveOutMessage(UlongToHandle(wod),DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,ds_hw_accel));
380         if (err == DS_OK) {
381             TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
382                   debugstr_guid(&DSOUND_renderer_guids[wod]),desc.szDesc,desc.szDrvname,lpContext);
383             MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
384                                  wDesc, sizeof(wDesc)/sizeof(WCHAR) );
385             wDesc[(sizeof(wDesc)/sizeof(WCHAR)) - 1] = '\0';
386
387             MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
388                                  wName, sizeof(wName)/sizeof(WCHAR) );
389             wName[(sizeof(wName)/sizeof(WCHAR)) - 1] = '\0';
390
391             if (lpDSEnumCallback(&DSOUND_renderer_guids[wod], wDesc, wName, lpContext) == FALSE)
392                 return DS_OK;
393         }
394     }
395     return DS_OK;
396 }
397
398 /***************************************************************************
399  * DirectSoundCaptureEnumerateA [DSOUND.7]
400  *
401  * Enumerate all DirectSound drivers installed in the system.
402  *
403  * PARAMS
404  *    lpDSEnumCallback  [I] Address of callback function.
405  *    lpContext         [I] Address of user defined context passed to callback function.
406  *
407  * RETURNS
408  *    Success: DS_OK
409  *    Failure: DSERR_INVALIDPARAM
410  */
411 HRESULT WINAPI DirectSoundCaptureEnumerateA(
412     LPDSENUMCALLBACKA lpDSEnumCallback,
413     LPVOID lpContext)
414 {
415     struct morecontext context;
416
417     if (lpDSEnumCallback == NULL) {
418         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
419         return DSERR_INVALIDPARAM;
420     }
421
422     context.callA = lpDSEnumCallback;
423     context.data = lpContext;
424
425     return DirectSoundCaptureEnumerateW(a_to_w_callback, &context);
426 }
427
428 /***************************************************************************
429  * DirectSoundCaptureEnumerateW [DSOUND.8]
430  *
431  * Enumerate all DirectSound drivers installed in the system.
432  *
433  * PARAMS
434  *    lpDSEnumCallback  [I] Address of callback function.
435  *    lpContext         [I] Address of user defined context passed to callback function.
436  *
437  * RETURNS
438  *    Success: DS_OK
439  *    Failure: DSERR_INVALIDPARAM
440  */
441 HRESULT WINAPI
442 DirectSoundCaptureEnumerateW(
443     LPDSENUMCALLBACKW lpDSEnumCallback,
444     LPVOID lpContext)
445 {
446     unsigned devs, wid;
447     DSDRIVERDESC desc;
448     GUID guid;
449     int err;
450     WCHAR wDesc[MAXPNAMELEN];
451     WCHAR wName[MAXPNAMELEN];
452
453     TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
454
455     if (lpDSEnumCallback == NULL) {
456         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
457         return DSERR_INVALIDPARAM;
458     }
459
460     setup_dsound_options();
461
462     devs = waveInGetNumDevs();
463     if (devs > 0) {
464         if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
465             for (wid = 0; wid < devs; ++wid) {
466                 if (IsEqualGUID( &guid, &DSOUND_capture_guids[wid] ) ) {
467                     err = mmErr(waveInMessage(UlongToHandle(wid),DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,ds_hw_accel));
468                     if (err == DS_OK) {
469                         TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
470                               "Primary Sound Capture Driver",desc.szDrvname,lpContext);
471                         MultiByteToWideChar( CP_ACP, 0, "Primary Sound Capture Driver", -1,
472                                              wDesc, sizeof(wDesc)/sizeof(WCHAR) );
473                         MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
474                                              wName, sizeof(wName)/sizeof(WCHAR) );
475                         wName[(sizeof(wName)/sizeof(WCHAR)) - 1] = '\0';
476
477                         if (lpDSEnumCallback(NULL, wDesc, wName, lpContext) == FALSE)
478                             return DS_OK;
479                     }
480                 }
481             }
482         }
483     }
484
485     for (wid = 0; wid < devs; ++wid) {
486         err = mmErr(waveInMessage(UlongToHandle(wid),DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,ds_hw_accel));
487         if (err == DS_OK) {
488             TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
489                   debugstr_guid(&DSOUND_capture_guids[wid]),desc.szDesc,desc.szDrvname,lpContext);
490             MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
491                                  wDesc, sizeof(wDesc)/sizeof(WCHAR) );
492             wDesc[(sizeof(wDesc)/sizeof(WCHAR)) - 1] = '\0';
493
494             MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
495                                  wName, sizeof(wName)/sizeof(WCHAR) );
496             wName[(sizeof(wName)/sizeof(WCHAR)) - 1] = '\0';
497
498             if (lpDSEnumCallback(&DSOUND_capture_guids[wid], wDesc, wName, lpContext) == FALSE)
499                 return DS_OK;
500         }
501     }
502
503     return DS_OK;
504 }
505
506 /*******************************************************************************
507  * DirectSound ClassFactory
508  */
509
510 typedef  HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
511  
512 typedef struct {
513     IClassFactory IClassFactory_iface;
514     REFCLSID rclsid;
515     FnCreateInstance pfnCreateInstance;
516 } IClassFactoryImpl;
517
518 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
519 {
520     return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
521 }
522
523 static HRESULT WINAPI
524 DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
525 {
526     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
527     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
528     if (ppobj == NULL)
529         return E_POINTER;
530     if (IsEqualIID(riid, &IID_IUnknown) ||
531         IsEqualIID(riid, &IID_IClassFactory))
532     {
533         *ppobj = iface;
534         IUnknown_AddRef(iface);
535         return S_OK;
536     }
537     *ppobj = NULL;
538     return E_NOINTERFACE;
539 }
540
541 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
542 {
543     return 2;
544 }
545
546 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
547 {
548     /* static class, won't be freed */
549     return 1;
550 }
551
552 static HRESULT WINAPI DSCF_CreateInstance(
553     LPCLASSFACTORY iface,
554     LPUNKNOWN pOuter,
555     REFIID riid,
556     LPVOID *ppobj)
557 {
558     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
559     TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
560
561     if (pOuter)
562         return CLASS_E_NOAGGREGATION;
563
564     if (ppobj == NULL) {
565         WARN("invalid parameter\n");
566         return DSERR_INVALIDPARAM;
567     }
568     *ppobj = NULL;
569     return This->pfnCreateInstance(riid, ppobj);
570 }
571  
572 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
573 {
574     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
575     FIXME("(%p, %d) stub!\n", This, dolock);
576     return S_OK;
577 }
578
579 static const IClassFactoryVtbl DSCF_Vtbl = {
580     DSCF_QueryInterface,
581     DSCF_AddRef,
582     DSCF_Release,
583     DSCF_CreateInstance,
584     DSCF_LockServer
585 };
586
587 static IClassFactoryImpl DSOUND_CF[] = {
588     { { &DSCF_Vtbl }, &CLSID_DirectSound, (FnCreateInstance)DSOUND_Create },
589     { { &DSCF_Vtbl }, &CLSID_DirectSound8, (FnCreateInstance)DSOUND_Create8 },
590     { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture, (FnCreateInstance)DSOUND_CaptureCreate },
591     { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture8, (FnCreateInstance)DSOUND_CaptureCreate8 },
592     { { &DSCF_Vtbl }, &CLSID_DirectSoundFullDuplex, (FnCreateInstance)DSOUND_FullDuplexCreate },
593     { { &DSCF_Vtbl }, &CLSID_DirectSoundPrivate, (FnCreateInstance)IKsPrivatePropertySetImpl_Create },
594     { { NULL }, NULL, NULL }
595 };
596
597 /*******************************************************************************
598  * DllGetClassObject [DSOUND.@]
599  * Retrieves class object from a DLL object
600  *
601  * NOTES
602  *    Docs say returns STDAPI
603  *
604  * PARAMS
605  *    rclsid [I] CLSID for the class object
606  *    riid   [I] Reference to identifier of interface for class object
607  *    ppv    [O] Address of variable to receive interface pointer for riid
608  *
609  * RETURNS
610  *    Success: S_OK
611  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
612  *             E_UNEXPECTED
613  */
614 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
615 {
616     int i = 0;
617     TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
618
619     if (ppv == NULL) {
620         WARN("invalid parameter\n");
621         return E_INVALIDARG;
622     }
623
624     *ppv = NULL;
625
626     if (!IsEqualIID(riid, &IID_IClassFactory) &&
627         !IsEqualIID(riid, &IID_IUnknown)) {
628         WARN("no interface for %s\n", debugstr_guid(riid));
629         return E_NOINTERFACE;
630     }
631
632     while (NULL != DSOUND_CF[i].rclsid) {
633         if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
634             DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
635             *ppv = &DSOUND_CF[i];
636             return S_OK;
637         }
638         i++;
639     }
640
641     WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
642          debugstr_guid(riid), ppv);
643     return CLASS_E_CLASSNOTAVAILABLE;
644 }
645
646
647 /*******************************************************************************
648  * DllCanUnloadNow [DSOUND.4]
649  * Determines whether the DLL is in use.
650  *
651  * RETURNS
652  *    Can unload now: S_OK
653  *    Cannot unload now (the DLL is still active): S_FALSE
654  */
655 HRESULT WINAPI DllCanUnloadNow(void)
656 {
657     return S_FALSE;
658 }
659
660 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)      \
661         guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2;               \
662         guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3;     \
663         guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6;     \
664         guid.Data4[6] = b7; guid.Data4[7] = b8;
665
666 /***********************************************************************
667  *           DllMain (DSOUND.init)
668  */
669 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
670 {
671     int i;
672     TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpvReserved);
673
674     switch (fdwReason) {
675     case DLL_PROCESS_ATTACH:
676         TRACE("DLL_PROCESS_ATTACH\n");
677         for (i = 0; i < MAXWAVEDRIVERS; i++) {
678             DSOUND_renderer[i] = NULL;
679             DSOUND_capture[i] = NULL;
680             INIT_GUID(DSOUND_renderer_guids[i], 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
681             INIT_GUID(DSOUND_capture_guids[i],  0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
682         }
683         instance = hInstDLL;
684         DisableThreadLibraryCalls(hInstDLL);
685         /* Increase refcount on dsound by 1 */
686         GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
687         break;
688     case DLL_PROCESS_DETACH:
689         TRACE("DLL_PROCESS_DETACH\n");
690         break;
691     default:
692         TRACE("UNKNOWN REASON\n");
693         break;
694     }
695     return TRUE;
696 }
697
698 /***********************************************************************
699  *              DllRegisterServer (DSOUND.@)
700  */
701 HRESULT WINAPI DllRegisterServer(void)
702 {
703     return __wine_register_resources( instance );
704 }
705
706 /***********************************************************************
707  *              DllUnregisterServer (DSOUND.@)
708  */
709 HRESULT WINAPI DllUnregisterServer(void)
710 {
711     return __wine_unregister_resources( instance );
712 }