msvcrt: Added more read tests.
[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 "rpc.h"
54 #include "rpcndr.h"
55 #include "unknwn.h"
56 #include "oleidl.h"
57 #include "shobjidl.h"
58
59 #include "initguid.h"
60 #include "ksmedia.h"
61 #include "propkey.h"
62 #include "devpkey.h"
63
64 #include "dsound_private.h"
65
66 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
67
68 struct list DSOUND_renderers = LIST_INIT(DSOUND_renderers);
69 CRITICAL_SECTION DSOUND_renderers_lock;
70 static CRITICAL_SECTION_DEBUG DSOUND_renderers_lock_debug =
71 {
72     0, 0, &DSOUND_renderers_lock,
73     { &DSOUND_renderers_lock_debug.ProcessLocksList, &DSOUND_renderers_lock_debug.ProcessLocksList },
74       0, 0, { (DWORD_PTR)(__FILE__ ": DSOUND_renderers_lock") }
75 };
76 CRITICAL_SECTION DSOUND_renderers_lock = { &DSOUND_renderers_lock_debug, -1, 0, 0, 0, 0 };
77
78 struct list DSOUND_capturers = LIST_INIT(DSOUND_capturers);
79 CRITICAL_SECTION DSOUND_capturers_lock;
80 static CRITICAL_SECTION_DEBUG DSOUND_capturers_lock_debug =
81 {
82     0, 0, &DSOUND_capturers_lock,
83     { &DSOUND_capturers_lock_debug.ProcessLocksList, &DSOUND_capturers_lock_debug.ProcessLocksList },
84       0, 0, { (DWORD_PTR)(__FILE__ ": DSOUND_capturers_lock") }
85 };
86 CRITICAL_SECTION DSOUND_capturers_lock = { &DSOUND_capturers_lock_debug, -1, 0, 0, 0, 0 };
87
88 GUID                    DSOUND_renderer_guids[MAXWAVEDRIVERS];
89 GUID                    DSOUND_capture_guids[MAXWAVEDRIVERS];
90
91 WCHAR wine_vxd_drv[] = { 'w','i','n','e','m','m','.','v','x','d', 0 };
92
93 /* All default settings, you most likely don't want to touch these, see wiki on UsefulRegistryKeys */
94 int ds_hel_buflen = 32768 * 2;
95 int ds_snd_queue_max = 10;
96 static HINSTANCE instance;
97
98 /*
99  * Get a config key from either the app-specific or the default config
100  */
101
102 static inline DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
103                                     char *buffer, DWORD size )
104 {
105     if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
106     if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
107     return ERROR_FILE_NOT_FOUND;
108 }
109
110
111 /*
112  * Setup the dsound options.
113  */
114
115 void setup_dsound_options(void)
116 {
117     char buffer[MAX_PATH+16];
118     HKEY hkey, appkey = 0;
119     DWORD len;
120
121     buffer[MAX_PATH]='\0';
122
123     /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
124     if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectSound", &hkey )) hkey = 0;
125
126     len = GetModuleFileNameA( 0, buffer, MAX_PATH );
127     if (len && len < MAX_PATH)
128     {
129         HKEY tmpkey;
130         /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
131         if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
132         {
133             char *p, *appname = buffer;
134             if ((p = strrchr( appname, '/' ))) appname = p + 1;
135             if ((p = strrchr( appname, '\\' ))) appname = p + 1;
136             strcat( appname, "\\DirectSound" );
137             TRACE("appname = [%s]\n", appname);
138             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
139             RegCloseKey( tmpkey );
140         }
141     }
142
143     /* get options */
144
145     if (!get_config_key( hkey, appkey, "HelBuflen", buffer, MAX_PATH ))
146         ds_hel_buflen = atoi(buffer);
147
148     if (!get_config_key( hkey, appkey, "SndQueueMax", buffer, MAX_PATH ))
149         ds_snd_queue_max = atoi(buffer);
150
151
152     if (appkey) RegCloseKey( appkey );
153     if (hkey) RegCloseKey( hkey );
154
155     TRACE("ds_hel_buflen = %d\n", ds_hel_buflen);
156     TRACE("ds_snd_queue_max = %d\n", ds_snd_queue_max);
157 }
158
159 static const char * get_device_id(LPCGUID pGuid)
160 {
161     if (IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
162         return "DSDEVID_DefaultPlayback";
163     else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
164         return "DSDEVID_DefaultVoicePlayback";
165     else if (IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
166         return "DSDEVID_DefaultCapture";
167     else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
168         return "DSDEVID_DefaultVoiceCapture";
169     return debugstr_guid(pGuid);
170 }
171
172 static HRESULT get_mmdevenum(IMMDeviceEnumerator **devenum)
173 {
174     HRESULT hr, init_hr;
175
176     init_hr = CoInitialize(NULL);
177
178     hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
179             CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)devenum);
180     if(FAILED(hr)){
181         CoUninitialize();
182         *devenum = NULL;
183         ERR("CoCreateInstance failed: %08x\n", hr);
184         return hr;
185     }
186
187     return init_hr;
188 }
189
190 static void release_mmdevenum(IMMDeviceEnumerator *devenum, HRESULT init_hr)
191 {
192     IMMDeviceEnumerator_Release(devenum);
193     if(SUCCEEDED(init_hr))
194         CoUninitialize();
195 }
196
197 static HRESULT get_mmdevice_guid(IMMDevice *device, IPropertyStore *ps,
198         GUID *guid)
199 {
200     PROPVARIANT pv;
201     HRESULT hr;
202
203     if(!ps){
204         hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
205         if(FAILED(hr)){
206             WARN("OpenPropertyStore failed: %08x\n", hr);
207             return hr;
208         }
209     }else
210         IPropertyStore_AddRef(ps);
211
212     PropVariantInit(&pv);
213
214     hr = IPropertyStore_GetValue(ps, &PKEY_AudioEndpoint_GUID, &pv);
215     if(FAILED(hr)){
216         IPropertyStore_Release(ps);
217         WARN("GetValue(GUID) failed: %08x\n", hr);
218         return hr;
219     }
220
221     CLSIDFromString(pv.u.pwszVal, guid);
222
223     PropVariantClear(&pv);
224     IPropertyStore_Release(ps);
225
226     return S_OK;
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     IMMDeviceEnumerator *devenum;
252     EDataFlow flow = (EDataFlow)-1;
253     ERole role = (ERole)-1;
254     HRESULT hr, init_hr;
255
256     TRACE("(%s,%p)\n", get_device_id(pGuidSrc),pGuidDest);
257
258     if(!pGuidSrc || !pGuidDest)
259         return DSERR_INVALIDPARAM;
260
261     init_hr = get_mmdevenum(&devenum);
262     if(!devenum)
263         return init_hr;
264
265     if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc)){
266         role = eMultimedia;
267         flow = eRender;
268     }else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc)){
269         role = eCommunications;
270         flow = eRender;
271     }else if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc)){
272         role = eMultimedia;
273         flow = eCapture;
274     }else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc)){
275         role = eCommunications;
276         flow = eCapture;
277     }
278
279     if(role != (ERole)-1 && flow != (EDataFlow)-1){
280         IMMDevice *device;
281
282         hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum,
283                 flow, role, &device);
284         if(FAILED(hr)){
285             WARN("GetDefaultAudioEndpoint failed: %08x\n", hr);
286             release_mmdevenum(devenum, init_hr);
287             return DSERR_NODRIVER;
288         }
289
290         hr = get_mmdevice_guid(device, NULL, pGuidDest);
291         IMMDevice_Release(device);
292
293         release_mmdevenum(devenum, init_hr);
294
295         return (hr == S_OK) ? DS_OK : hr;
296     }
297
298     release_mmdevenum(devenum, init_hr);
299
300     *pGuidDest = *pGuidSrc;
301
302     return DS_OK;
303 }
304
305 struct morecontext
306 {
307     LPDSENUMCALLBACKA callA;
308     LPVOID data;
309 };
310
311 static BOOL CALLBACK a_to_w_callback(LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
312 {
313     struct morecontext *context = data;
314     char descA[MAXPNAMELEN], modA[MAXPNAMELEN];
315
316     WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, sizeof(descA), NULL, NULL);
317     WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, sizeof(modA), NULL, NULL);
318
319     return context->callA(guid, descA, modA, context->data);
320 }
321
322 /***************************************************************************
323  * DirectSoundEnumerateA [DSOUND.2]
324  *
325  * Enumerate all DirectSound drivers installed in the system
326  *
327  * PARAMS
328  *    lpDSEnumCallback  [I] Address of callback function.
329  *    lpContext         [I] Address of user defined context passed to callback function.
330  *
331  * RETURNS
332  *    Success: DS_OK
333  *    Failure: DSERR_INVALIDPARAM
334  */
335 HRESULT WINAPI DirectSoundEnumerateA(
336     LPDSENUMCALLBACKA lpDSEnumCallback,
337     LPVOID lpContext)
338 {
339     struct morecontext context;
340
341     if (lpDSEnumCallback == NULL) {
342         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
343         return DSERR_INVALIDPARAM;
344     }
345
346     context.callA = lpDSEnumCallback;
347     context.data = lpContext;
348
349     return DirectSoundEnumerateW(a_to_w_callback, &context);
350 }
351
352 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
353 {
354     IMMDeviceEnumerator *devenum;
355     IMMDeviceCollection *coll;
356     UINT count, i;
357     HRESULT hr, init_hr;
358
359     init_hr = get_mmdevenum(&devenum);
360     if(!devenum)
361         return init_hr;
362
363     hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
364             DEVICE_STATE_ACTIVE, &coll);
365     if(FAILED(hr)){
366         WARN("EnumAudioEndpoints failed: %08x\n", hr);
367         release_mmdevenum(devenum, init_hr);
368         return hr;
369     }
370
371     hr = IMMDeviceCollection_GetCount(coll, &count);
372     if(FAILED(hr)){
373         IMMDeviceCollection_Release(coll);
374         release_mmdevenum(devenum, init_hr);
375         WARN("GetCount failed: %08x\n", hr);
376         return hr;
377     }
378
379     for(i = 0; i < count; ++i){
380         GUID guid;
381
382         hr = IMMDeviceCollection_Item(coll, i, device);
383         if(FAILED(hr))
384             continue;
385
386         hr = get_mmdevice_guid(*device, NULL, &guid);
387         if(FAILED(hr)){
388             IMMDevice_Release(*device);
389             continue;
390         }
391
392         if(IsEqualGUID(&guid, tgt)){
393             IMMDeviceCollection_Release(coll);
394             release_mmdevenum(devenum, init_hr);
395             return DS_OK;
396         }
397
398         IMMDevice_Release(*device);
399     }
400
401     WARN("No device with GUID %s found!\n", wine_dbgstr_guid(tgt));
402
403     IMMDeviceCollection_Release(coll);
404     release_mmdevenum(devenum, init_hr);
405
406     return DSERR_INVALIDPARAM;
407 }
408
409 static BOOL send_device(IMMDevice *device, GUID *guid,
410         LPDSENUMCALLBACKW cb, void *user)
411 {
412     IPropertyStore *ps;
413     PROPVARIANT pv;
414     BOOL keep_going;
415     HRESULT hr;
416
417     PropVariantInit(&pv);
418
419     hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
420     if(FAILED(hr)){
421         WARN("OpenPropertyStore failed: %08x\n", hr);
422         return TRUE;
423     }
424
425     hr = get_mmdevice_guid(device, ps, guid);
426     if(FAILED(hr)){
427         IPropertyStore_Release(ps);
428         return TRUE;
429     }
430
431     hr = IPropertyStore_GetValue(ps,
432             (const PROPERTYKEY *)&DEVPKEY_Device_FriendlyName, &pv);
433     if(FAILED(hr)){
434         IPropertyStore_Release(ps);
435         WARN("GetValue(FriendlyName) failed: %08x\n", hr);
436         return TRUE;
437     }
438
439     TRACE("Calling back with %s (%s)\n", wine_dbgstr_guid(guid),
440             wine_dbgstr_w(pv.u.pwszVal));
441
442     keep_going = cb(guid, pv.u.pwszVal, wine_vxd_drv, user);
443
444     PropVariantClear(&pv);
445     IPropertyStore_Release(ps);
446
447     return keep_going;
448 }
449
450 /* S_FALSE means the callback returned FALSE at some point
451  * S_OK means the callback always returned TRUE */
452 HRESULT enumerate_mmdevices(EDataFlow flow, GUID *guids,
453         LPDSENUMCALLBACKW cb, void *user)
454 {
455     IMMDeviceEnumerator *devenum;
456     IMMDeviceCollection *coll;
457     IMMDevice *defdev = NULL;
458     UINT count, i, n;
459     BOOL keep_going;
460     HRESULT hr, init_hr;
461
462     static const WCHAR primary_desc[] = {'P','r','i','m','a','r','y',' ',
463         'S','o','u','n','d',' ','D','r','i','v','e','r',0};
464     static const WCHAR empty_drv[] = {0};
465
466     init_hr = get_mmdevenum(&devenum);
467     if(!devenum)
468         return init_hr;
469
470     hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
471             DEVICE_STATE_ACTIVE, &coll);
472     if(FAILED(hr)){
473         release_mmdevenum(devenum, init_hr);
474         WARN("EnumAudioEndpoints failed: %08x\n", hr);
475         return DS_OK;
476     }
477
478     hr = IMMDeviceCollection_GetCount(coll, &count);
479     if(FAILED(hr)){
480         IMMDeviceCollection_Release(coll);
481         release_mmdevenum(devenum, init_hr);
482         WARN("GetCount failed: %08x\n", hr);
483         return DS_OK;
484     }
485
486     if(count == 0){
487         release_mmdevenum(devenum, init_hr);
488         return DS_OK;
489     }
490
491     TRACE("Calling back with NULL (%s)\n", wine_dbgstr_w(primary_desc));
492     keep_going = cb(NULL, primary_desc, empty_drv, user);
493
494     /* always send the default device first */
495     if(keep_going){
496         hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow,
497                 eMultimedia, &defdev);
498         if(FAILED(hr)){
499             defdev = NULL;
500             n = 0;
501         }else{
502             keep_going = send_device(defdev, &guids[0], cb, user);
503             n = 1;
504         }
505     }
506
507     for(i = 0; keep_going && i < count; ++i){
508         IMMDevice *device;
509
510         hr = IMMDeviceCollection_Item(coll, i, &device);
511         if(FAILED(hr)){
512             WARN("Item failed: %08x\n", hr);
513             continue;
514         }
515
516         if(device != defdev){
517             send_device(device, &guids[n], cb, user);
518             ++n;
519         }
520
521         IMMDevice_Release(device);
522     }
523
524     if(defdev)
525         IMMDevice_Release(defdev);
526     IMMDeviceCollection_Release(coll);
527
528     release_mmdevenum(devenum, init_hr);
529
530     return (keep_going == TRUE) ? S_OK : S_FALSE;
531 }
532
533 /***************************************************************************
534  * DirectSoundEnumerateW [DSOUND.3]
535  *
536  * Enumerate all DirectSound drivers installed in the system
537  *
538  * PARAMS
539  *    lpDSEnumCallback  [I] Address of callback function.
540  *    lpContext         [I] Address of user defined context passed to callback function.
541  *
542  * RETURNS
543  *    Success: DS_OK
544  *    Failure: DSERR_INVALIDPARAM
545  */
546 HRESULT WINAPI DirectSoundEnumerateW(
547         LPDSENUMCALLBACKW lpDSEnumCallback,
548         LPVOID lpContext )
549 {
550     HRESULT hr;
551
552     TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext);
553
554     if (lpDSEnumCallback == NULL) {
555         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
556         return DSERR_INVALIDPARAM;
557     }
558
559     setup_dsound_options();
560
561     hr = enumerate_mmdevices(eRender, DSOUND_renderer_guids,
562             lpDSEnumCallback, lpContext);
563     return SUCCEEDED(hr) ? DS_OK : hr;
564 }
565
566 /***************************************************************************
567  * DirectSoundCaptureEnumerateA [DSOUND.7]
568  *
569  * Enumerate all DirectSound drivers installed in the system.
570  *
571  * PARAMS
572  *    lpDSEnumCallback  [I] Address of callback function.
573  *    lpContext         [I] Address of user defined context passed to callback function.
574  *
575  * RETURNS
576  *    Success: DS_OK
577  *    Failure: DSERR_INVALIDPARAM
578  */
579 HRESULT WINAPI DirectSoundCaptureEnumerateA(
580     LPDSENUMCALLBACKA lpDSEnumCallback,
581     LPVOID lpContext)
582 {
583     struct morecontext context;
584
585     if (lpDSEnumCallback == NULL) {
586         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
587         return DSERR_INVALIDPARAM;
588     }
589
590     context.callA = lpDSEnumCallback;
591     context.data = lpContext;
592
593     return DirectSoundCaptureEnumerateW(a_to_w_callback, &context);
594 }
595
596 /***************************************************************************
597  * DirectSoundCaptureEnumerateW [DSOUND.8]
598  *
599  * Enumerate all DirectSound drivers installed in the system.
600  *
601  * PARAMS
602  *    lpDSEnumCallback  [I] Address of callback function.
603  *    lpContext         [I] Address of user defined context passed to callback function.
604  *
605  * RETURNS
606  *    Success: DS_OK
607  *    Failure: DSERR_INVALIDPARAM
608  */
609 HRESULT WINAPI
610 DirectSoundCaptureEnumerateW(
611     LPDSENUMCALLBACKW lpDSEnumCallback,
612     LPVOID lpContext)
613 {
614     HRESULT hr;
615
616     TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
617
618     if (lpDSEnumCallback == NULL) {
619         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
620         return DSERR_INVALIDPARAM;
621     }
622
623     setup_dsound_options();
624
625     hr = enumerate_mmdevices(eCapture, DSOUND_capture_guids,
626             lpDSEnumCallback, lpContext);
627     return SUCCEEDED(hr) ? DS_OK : hr;
628 }
629
630 /*******************************************************************************
631  * DirectSound ClassFactory
632  */
633
634 typedef  HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
635  
636 typedef struct {
637     IClassFactory IClassFactory_iface;
638     REFCLSID rclsid;
639     FnCreateInstance pfnCreateInstance;
640 } IClassFactoryImpl;
641
642 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
643 {
644     return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
645 }
646
647 static HRESULT WINAPI
648 DSCF_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppobj)
649 {
650     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
651     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
652     if (ppobj == NULL)
653         return E_POINTER;
654     if (IsEqualIID(riid, &IID_IUnknown) ||
655         IsEqualIID(riid, &IID_IClassFactory))
656     {
657         *ppobj = iface;
658         IClassFactory_AddRef(iface);
659         return S_OK;
660     }
661     *ppobj = NULL;
662     return E_NOINTERFACE;
663 }
664
665 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
666 {
667     return 2;
668 }
669
670 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
671 {
672     /* static class, won't be freed */
673     return 1;
674 }
675
676 static HRESULT WINAPI DSCF_CreateInstance(
677     LPCLASSFACTORY iface,
678     LPUNKNOWN pOuter,
679     REFIID riid,
680     LPVOID *ppobj)
681 {
682     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
683     TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
684
685     if (pOuter)
686         return CLASS_E_NOAGGREGATION;
687
688     if (ppobj == NULL) {
689         WARN("invalid parameter\n");
690         return DSERR_INVALIDPARAM;
691     }
692     *ppobj = NULL;
693     return This->pfnCreateInstance(riid, ppobj);
694 }
695  
696 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
697 {
698     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
699     FIXME("(%p, %d) stub!\n", This, dolock);
700     return S_OK;
701 }
702
703 static const IClassFactoryVtbl DSCF_Vtbl = {
704     DSCF_QueryInterface,
705     DSCF_AddRef,
706     DSCF_Release,
707     DSCF_CreateInstance,
708     DSCF_LockServer
709 };
710
711 static IClassFactoryImpl DSOUND_CF[] = {
712     { { &DSCF_Vtbl }, &CLSID_DirectSound, DSOUND_Create },
713     { { &DSCF_Vtbl }, &CLSID_DirectSound8, DSOUND_Create8 },
714     { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
715     { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
716     { { &DSCF_Vtbl }, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
717     { { &DSCF_Vtbl }, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
718     { { NULL }, NULL, NULL }
719 };
720
721 /*******************************************************************************
722  * DllGetClassObject [DSOUND.@]
723  * Retrieves class object from a DLL object
724  *
725  * NOTES
726  *    Docs say returns STDAPI
727  *
728  * PARAMS
729  *    rclsid [I] CLSID for the class object
730  *    riid   [I] Reference to identifier of interface for class object
731  *    ppv    [O] Address of variable to receive interface pointer for riid
732  *
733  * RETURNS
734  *    Success: S_OK
735  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
736  *             E_UNEXPECTED
737  */
738 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
739 {
740     int i = 0;
741     TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
742
743     if (ppv == NULL) {
744         WARN("invalid parameter\n");
745         return E_INVALIDARG;
746     }
747
748     *ppv = NULL;
749
750     if (!IsEqualIID(riid, &IID_IClassFactory) &&
751         !IsEqualIID(riid, &IID_IUnknown)) {
752         WARN("no interface for %s\n", debugstr_guid(riid));
753         return E_NOINTERFACE;
754     }
755
756     while (NULL != DSOUND_CF[i].rclsid) {
757         if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
758             DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
759             *ppv = &DSOUND_CF[i];
760             return S_OK;
761         }
762         i++;
763     }
764
765     WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
766          debugstr_guid(riid), ppv);
767     return CLASS_E_CLASSNOTAVAILABLE;
768 }
769
770
771 /*******************************************************************************
772  * DllCanUnloadNow [DSOUND.4]
773  * Determines whether the DLL is in use.
774  *
775  * RETURNS
776  *    Can unload now: S_OK
777  *    Cannot unload now (the DLL is still active): S_FALSE
778  */
779 HRESULT WINAPI DllCanUnloadNow(void)
780 {
781     return S_FALSE;
782 }
783
784 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)      \
785         guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2;               \
786         guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3;     \
787         guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6;     \
788         guid.Data4[6] = b7; guid.Data4[7] = b8;
789
790 /***********************************************************************
791  *           DllMain (DSOUND.init)
792  */
793 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
794 {
795     TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpvReserved);
796
797     switch (fdwReason) {
798     case DLL_PROCESS_ATTACH:
799         TRACE("DLL_PROCESS_ATTACH\n");
800         instance = hInstDLL;
801         DisableThreadLibraryCalls(hInstDLL);
802         /* Increase refcount on dsound by 1 */
803         GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
804         break;
805     case DLL_PROCESS_DETACH:
806         TRACE("DLL_PROCESS_DETACH\n");
807         DeleteCriticalSection(&DSOUND_renderers_lock);
808         DeleteCriticalSection(&DSOUND_capturers_lock);
809         break;
810     default:
811         TRACE("UNKNOWN REASON\n");
812         break;
813     }
814     return TRUE;
815 }
816
817 /***********************************************************************
818  *              DllRegisterServer (DSOUND.@)
819  */
820 HRESULT WINAPI DllRegisterServer(void)
821 {
822     return __wine_register_resources( instance );
823 }
824
825 /***********************************************************************
826  *              DllUnregisterServer (DSOUND.@)
827  */
828 HRESULT WINAPI DllUnregisterServer(void)
829 {
830     return __wine_unregister_resources( instance );
831 }