Be more stringent in the 'Lock' invalid RECT check.
[wine] / dlls / dsound / dsound_main.c
1 /*                      DirectSound
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998 Rob Riggs
5  * Copyright 2000-2002 TransGaming Technologies, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 /*
22  * Most thread locking is complete. There may be a few race
23  * conditions still lurking.
24  *
25  * Tested with a Soundblaster clone, a Gravis UltraSound Classic,
26  * and a Turtle Beach Tropez+.
27  *
28  * TODO:
29  *      Implement SetCooperativeLevel properly (need to address focus issues)
30  *      Implement DirectSound3DBuffers (stubs in place)
31  *      Use hardware 3D support if available
32  *      Add critical section locking inside Release and AddRef methods
33  *      Handle static buffers - put those in hardware, non-static not in hardware
34  *      Hardware DuplicateSoundBuffer
35  *      Proper volume calculation, and setting volume in HEL primary buffer
36  *      Optimize WINMM and negotiate fragment size, decrease DS_HEL_MARGIN
37  */
38
39 #include "config.h"
40 #include "wine/port.h"
41
42 #include <assert.h>
43 #include <stdio.h>
44 #include <sys/types.h>
45 #include <sys/fcntl.h>
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
49 #include <stdlib.h>
50 #include <string.h>
51 #include <math.h>       /* Insomnia - pow() function */
52
53 #include "windef.h"
54 #include "winbase.h"
55 #include "winreg.h"
56 #include "winuser.h"
57 #include "wingdi.h"
58 #include "winuser.h"
59 #include "winerror.h"
60 #include "mmsystem.h"
61 #include "winternl.h"
62 #include "mmddk.h"
63 #include "wine/windef16.h"
64 #include "wine/winbase16.h"
65 #include "wine/debug.h"
66 #include "dsound.h"
67 #include "dsdriver.h"
68 #include "dsound_private.h"
69
70 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
71
72 /* these are eligible for tuning... they must be high on slow machines... */
73 /* some stuff may get more responsive with lower values though... */
74 #define DS_EMULDRIVER 0 /* some games (Quake 2, UT) refuse to accept
75                                 emulated dsound devices. set to 0 ! */
76 #define DS_HEL_MARGIN 5 /* HEL only: number of waveOut fragments ahead to mix in new buffers
77                          * (keep this close or equal to DS_HEL_QUEUE for best results) */
78 #define DS_HEL_QUEUE  5 /* HEL only: number of waveOut fragments ahead to queue to driver
79                          * (this will affect HEL sound reliability and latency) */
80
81 #define DS_SND_QUEUE_MAX 28 /* max number of fragments to prebuffer */
82 #define DS_SND_QUEUE_MIN 12 /* min number of fragments to prebuffer */
83
84 IDirectSoundImpl*       dsound = NULL;
85
86 HRESULT mmErr(UINT err)
87 {
88         switch(err) {
89         case MMSYSERR_NOERROR:
90                 return DS_OK;
91         case MMSYSERR_ALLOCATED:
92                 return DSERR_ALLOCATED;
93         case MMSYSERR_ERROR:
94         case MMSYSERR_INVALHANDLE:
95         case WAVERR_STILLPLAYING:
96                 return DSERR_GENERIC; /* FIXME */
97         case MMSYSERR_NODRIVER:
98                 return DSERR_NODRIVER;
99         case MMSYSERR_NOMEM:
100                 return DSERR_OUTOFMEMORY;
101         case MMSYSERR_INVALPARAM:
102         case WAVERR_BADFORMAT:
103         case WAVERR_UNPREPARED:
104                 return DSERR_INVALIDPARAM;
105         case MMSYSERR_NOTSUPPORTED:
106                 return DSERR_UNSUPPORTED;
107         default:
108                 FIXME("Unknown MMSYS error %d\n",err);
109                 return DSERR_GENERIC;
110         }
111 }
112
113 int ds_emuldriver = DS_EMULDRIVER;
114 int ds_hel_margin = DS_HEL_MARGIN;
115 int ds_hel_queue = DS_HEL_QUEUE;
116 int ds_snd_queue_max = DS_SND_QUEUE_MAX;
117 int ds_snd_queue_min = DS_SND_QUEUE_MIN;
118 int ds_hw_accel = DS_HW_ACCEL_FULL;
119 int ds_default_playback = 0;
120 int ds_default_capture = 0;
121
122 /*
123  * Get a config key from either the app-specific or the default config
124  */
125
126 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
127                                     char *buffer, DWORD size )
128 {
129     if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
130     return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
131 }
132
133
134 /*
135  * Setup the dsound options.
136  */
137
138 void setup_dsound_options(void)
139 {
140     char buffer[MAX_PATH+1];
141     HKEY hkey, appkey = 0;
142
143     buffer[MAX_PATH]='\0';
144
145     if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\dsound", 0, NULL,
146                          REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
147     {
148         ERR("Cannot create config registry key\n" );
149         ExitProcess(1);
150     }
151
152     if (GetModuleFileNameA( 0, buffer, MAX_PATH ))
153     {
154         HKEY tmpkey;
155
156         if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
157         {
158            char appname[MAX_PATH+16];
159            char *p = strrchr( buffer, '\\' );
160            if (p!=NULL) {
161                    appname[MAX_PATH]='\0';
162                    strncpy(appname,p+1,MAX_PATH);
163                    strcat(appname,"\\dsound");
164                    TRACE("appname = [%s] \n",appname);
165                    if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
166                        RegCloseKey( tmpkey );
167            }
168         }
169     }
170
171     /* get options */
172
173     if (!get_config_key( hkey, appkey, "EmulDriver", buffer, MAX_PATH ))
174         ds_emuldriver = strcmp(buffer, "N");
175
176     if (!get_config_key( hkey, appkey, "HELmargin", buffer, MAX_PATH ))
177         ds_hel_margin = atoi(buffer);
178
179     if (!get_config_key( hkey, appkey, "HELqueue", buffer, MAX_PATH ))
180         ds_hel_queue = atoi(buffer);
181
182     if (!get_config_key( hkey, appkey, "SndQueueMax", buffer, MAX_PATH ))
183         ds_snd_queue_max = atoi(buffer);
184
185     if (!get_config_key( hkey, appkey, "SndQueueMin", buffer, MAX_PATH ))
186         ds_snd_queue_min = atoi(buffer);
187
188     if (!get_config_key( hkey, appkey, "HardwareAcceleration", buffer, MAX_PATH )) {
189         if (strcmp(buffer, "Full") == 0)
190             ds_hw_accel = DS_HW_ACCEL_FULL;
191         else if (strcmp(buffer, "Standard") == 0)
192             ds_hw_accel = DS_HW_ACCEL_STANDARD;
193         else if (strcmp(buffer, "Basic") == 0)
194             ds_hw_accel = DS_HW_ACCEL_BASIC;
195         else if (strcmp(buffer, "Emulation") == 0)
196             ds_hw_accel = DS_HW_ACCEL_EMULATION;
197     }
198
199     if (!get_config_key( hkey, appkey, "DefaultPlayback", buffer, MAX_PATH ))
200             ds_default_playback = atoi(buffer);
201
202     if (!get_config_key( hkey, appkey, "DefaultCapture", buffer, MAX_PATH ))
203             ds_default_capture = atoi(buffer);
204
205     if (appkey) RegCloseKey( appkey );
206     RegCloseKey( hkey );
207
208     if (ds_emuldriver != DS_EMULDRIVER )
209        WARN("ds_emuldriver = %d (default=%d)\n",ds_emuldriver, DS_EMULDRIVER);
210     if (ds_hel_margin != DS_HEL_MARGIN )
211        WARN("ds_hel_margin = %d (default=%d)\n",ds_hel_margin, DS_HEL_MARGIN );
212     if (ds_hel_queue != DS_HEL_QUEUE )
213        WARN("ds_hel_queue = %d (default=%d)\n",ds_hel_queue, DS_HEL_QUEUE );
214     if (ds_snd_queue_max != DS_SND_QUEUE_MAX)
215        WARN("ds_snd_queue_max = %d (default=%d)\n",ds_snd_queue_max ,DS_SND_QUEUE_MAX);
216     if (ds_snd_queue_min != DS_SND_QUEUE_MIN)
217        WARN("ds_snd_queue_min = %d (default=%d)\n",ds_snd_queue_min ,DS_SND_QUEUE_MIN);
218     if (ds_hw_accel != DS_HW_ACCEL_FULL)
219         WARN("ds_hw_accel = %s (default=Full)\n", 
220             ds_hw_accel==DS_HW_ACCEL_FULL ? "Full" :
221             ds_hw_accel==DS_HW_ACCEL_STANDARD ? "Standard" :
222             ds_hw_accel==DS_HW_ACCEL_BASIC ? "Basic" :
223             ds_hw_accel==DS_HW_ACCEL_EMULATION ? "Emulation" :
224             "Unknown");
225     if (ds_default_playback != 0)
226         WARN("ds_default_playback = %d (default=0)\n",ds_default_playback);
227     if (ds_default_capture != 0)
228         WARN("ds_default_capture = %d (default=0)\n",ds_default_playback);
229 }
230
231
232
233 /***************************************************************************
234  * GetDeviceID  [DSOUND.9]
235  *
236  * Retrieves unique identifier of default device specified
237  *
238  * PARAMS
239  *    pGuidSrc  [I] Address of device GUID.
240  *    pGuidDest [O] Address to receive unique device GUID.
241  *
242  * RETURNS
243  *    Success: DS_OK
244  *    Failure: DSERR_INVALIDPARAM
245  *
246  * NOTES
247  *    pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
248  *    DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or 
249  *    DSDEVID_DefaultVoiceCapture.
250  *    Returns pGuidSrc if pGuidSrc is a valid device or the device
251  *    GUID for the specified constants.
252  */
253 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
254 {
255     TRACE("(%p,%p)\n",pGuidSrc,pGuidDest);
256
257     if ( pGuidSrc == NULL) {
258         WARN("invalid parameter: pGuidSrc == NULL\n");
259         return DSERR_INVALIDPARAM;
260     }
261
262     if ( pGuidDest == NULL ) {
263         WARN("invalid parameter: pGuidDest == NULL\n");
264         return DSERR_INVALIDPARAM;
265     }
266
267     if ( IsEqualGUID( &DSDEVID_DefaultPlayback, pGuidSrc ) ||
268         IsEqualGUID( &DSDEVID_DefaultVoicePlayback, pGuidSrc ) ) {
269         GUID guid;
270         int err = mmErr(waveOutMessage((HWAVEOUT)ds_default_playback,DRV_QUERYDSOUNDGUID,(DWORD)&guid,0));
271         if (err == DS_OK) {
272             memcpy(pGuidDest, &guid, sizeof(GUID));
273             return DS_OK;
274         }
275     }
276
277     if ( IsEqualGUID( &DSDEVID_DefaultCapture, pGuidSrc ) ||
278         IsEqualGUID( &DSDEVID_DefaultVoiceCapture, pGuidSrc ) ) {
279         GUID guid;
280         int err = mmErr(waveInMessage((HWAVEIN)ds_default_capture,DRV_QUERYDSOUNDGUID,(DWORD)&guid,0));
281         if (err == DS_OK) {
282             memcpy(pGuidDest, &guid, sizeof(GUID));
283             return DS_OK;
284         }
285     }
286
287     memcpy(pGuidDest, pGuidSrc, sizeof(GUID));
288
289     return DS_OK;
290 }
291
292
293 /***************************************************************************
294  * DirectSoundEnumerateA [DSOUND.2]
295  *
296  * Enumerate all DirectSound drivers installed in the system
297  *
298  * PARAMS
299  *    lpDSEnumCallback  [I] Address of callback function.
300  *    lpContext         [I] Address of user defined context passed to callback function.
301  *
302  * RETURNS
303  *    Success: DS_OK
304  *    Failure: DSERR_INVALIDPARAM
305  */
306 HRESULT WINAPI DirectSoundEnumerateA(
307     LPDSENUMCALLBACKA lpDSEnumCallback,
308     LPVOID lpContext)
309 {
310     unsigned devs, wod;
311     DSDRIVERDESC desc;
312     GUID guid;
313     int err;
314
315     TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
316         lpDSEnumCallback, lpContext);
317
318     if (lpDSEnumCallback == NULL) {
319         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
320         return DSERR_INVALIDPARAM;
321     }
322
323     devs = waveOutGetNumDevs();
324     if (devs > 0) {
325         if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
326             GUID temp;
327             for (wod = 0; wod < devs; ++wod) {
328                 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDGUID,(DWORD)&temp,0));
329                 if (err == DS_OK) {
330                     if (IsEqualGUID( &guid, &temp ) ) {
331                         err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
332                         if (err == DS_OK) {
333                             TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
334                                 debugstr_guid(&DSDEVID_DefaultPlayback),"Primary Sound Driver",desc.szDrvName,lpContext);
335                             if (lpDSEnumCallback((LPGUID)&DSDEVID_DefaultPlayback, "Primary Sound Driver", desc.szDrvName, lpContext) == FALSE)
336                                 return DS_OK;
337                         }
338                     }
339                 }
340             }
341         }
342     }
343
344     for (wod = 0; wod < devs; ++wod) {
345         err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
346         if (err == DS_OK) {
347             err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDGUID,(DWORD)&guid,0));
348             if (err == DS_OK) {
349                 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
350                     debugstr_guid(&guid),desc.szDesc,desc.szDrvName,lpContext);
351                 if (lpDSEnumCallback(&guid, desc.szDesc, desc.szDrvName, lpContext) == FALSE)
352                     return DS_OK;
353             }
354         }
355     }
356     return DS_OK;
357 }
358
359 /***************************************************************************
360  * DirectSoundEnumerateW [DSOUND.3]
361  *
362  * Enumerate all DirectSound drivers installed in the system
363  *
364  * PARAMS
365  *    lpDSEnumCallback  [I] Address of callback function.
366  *    lpContext         [I] Address of user defined context passed to callback function.
367  *
368  * RETURNS
369  *    Success: DS_OK
370  *    Failure: DSERR_INVALIDPARAM
371  */
372 HRESULT WINAPI DirectSoundEnumerateW(
373         LPDSENUMCALLBACKW lpDSEnumCallback,
374         LPVOID lpContext )
375 {
376     unsigned devs, wod;
377     DSDRIVERDESC desc;
378     GUID guid;
379     int err;
380     WCHAR wDesc[MAXPNAMELEN];
381     WCHAR wName[MAXPNAMELEN];
382
383     TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
384         lpDSEnumCallback, lpContext);
385
386     if (lpDSEnumCallback == NULL) {
387         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
388         return DSERR_INVALIDPARAM;
389     }
390
391     devs = waveOutGetNumDevs();
392     if (devs > 0) {
393         if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
394             GUID temp;
395             for (wod = 0; wod < devs; ++wod) {
396                 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDGUID,(DWORD)&temp,0));
397                 if (err == DS_OK) {
398                     if (IsEqualGUID( &guid, &temp ) ) {
399                         err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
400                         if (err == DS_OK) {
401                             TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
402                                 debugstr_guid(&DSDEVID_DefaultPlayback),"Primary Sound Driver",desc.szDrvName,lpContext);
403                             MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
404                                 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
405                                 MultiByteToWideChar( CP_ACP, 0, desc.szDrvName, -1,
406                                 wName, sizeof(wName)/sizeof(WCHAR) );
407                             if (lpDSEnumCallback((LPGUID)&DSDEVID_DefaultPlayback, wDesc, wName, lpContext) == FALSE)
408                                 return DS_OK;
409                         }
410                     }
411                 }
412             }
413         }
414     }
415
416     for (wod = 0; wod < devs; ++wod) {
417         err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
418         if (err == DS_OK) {
419             err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDGUID,(DWORD)&guid,0));
420             if (err == DS_OK) {
421                 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
422                     debugstr_guid(&guid),desc.szDesc,desc.szDrvName,lpContext);
423                 MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
424                     wDesc, sizeof(wDesc)/sizeof(WCHAR) );
425                     MultiByteToWideChar( CP_ACP, 0, desc.szDrvName, -1,
426                     wName, sizeof(wName)/sizeof(WCHAR) );
427                 if (lpDSEnumCallback(&guid, wDesc, wName, lpContext) == FALSE)
428                     return DS_OK;
429             }
430         }
431     }
432     return DS_OK;
433 }
434
435
436 static void _dump_DSBCAPS(DWORD xmask) {
437         struct {
438                 DWORD   mask;
439                 char    *name;
440         } flags[] = {
441 #define FE(x) { x, #x },
442                 FE(DSBCAPS_PRIMARYBUFFER)
443                 FE(DSBCAPS_STATIC)
444                 FE(DSBCAPS_LOCHARDWARE)
445                 FE(DSBCAPS_LOCSOFTWARE)
446                 FE(DSBCAPS_CTRL3D)
447                 FE(DSBCAPS_CTRLFREQUENCY)
448                 FE(DSBCAPS_CTRLPAN)
449                 FE(DSBCAPS_CTRLVOLUME)
450                 FE(DSBCAPS_CTRLPOSITIONNOTIFY)
451                 FE(DSBCAPS_CTRLDEFAULT)
452                 FE(DSBCAPS_CTRLALL)
453                 FE(DSBCAPS_STICKYFOCUS)
454                 FE(DSBCAPS_GLOBALFOCUS)
455                 FE(DSBCAPS_GETCURRENTPOSITION2)
456                 FE(DSBCAPS_MUTE3DATMAXDISTANCE)
457 #undef FE
458         };
459         int     i;
460
461         for (i=0;i<sizeof(flags)/sizeof(flags[0]);i++)
462                 if ((flags[i].mask & xmask) == flags[i].mask)
463                         DPRINTF("%s ",flags[i].name);
464 }
465
466 /*******************************************************************************
467  *              IDirectSound
468  */
469
470 static HRESULT WINAPI IDirectSoundImpl_SetCooperativeLevel(
471         LPDIRECTSOUND8 iface,HWND hwnd,DWORD level
472 ) {
473         ICOM_THIS(IDirectSoundImpl,iface);
474
475         FIXME("(%p,%08lx,%ld):stub\n",This,(DWORD)hwnd,level);
476
477         This->priolevel = level;
478
479         return DS_OK;
480 }
481
482 static HRESULT WINAPI IDirectSoundImpl_CreateSoundBuffer(
483         LPDIRECTSOUND8 iface,LPDSBUFFERDESC dsbd,LPLPDIRECTSOUNDBUFFER8 ppdsb,LPUNKNOWN lpunk
484 ) {
485         ICOM_THIS(IDirectSoundImpl,iface);
486         LPWAVEFORMATEX  wfex;
487         HRESULT hres;
488
489         TRACE("(%p,%p,%p,%p)\n",This,dsbd,ppdsb,lpunk);
490
491         if (This == NULL) {
492                 WARN("invalid parameter: This == NULL\n");
493                 return DSERR_INVALIDPARAM;
494         }
495
496         if (dsbd == NULL) {
497                 WARN("invalid parameter: dsbd == NULL\n");
498                 return DSERR_INVALIDPARAM;
499         }
500
501         if (ppdsb == NULL) {
502                 WARN("invalid parameter: ppdsb == NULL\n");
503                 return DSERR_INVALIDPARAM;
504         }
505
506         if (TRACE_ON(dsound)) {
507                 TRACE("(structsize=%ld)\n",dsbd->dwSize);
508                 TRACE("(flags=0x%08lx:\n",dsbd->dwFlags);
509                 _dump_DSBCAPS(dsbd->dwFlags);
510                 DPRINTF(")\n");
511                 TRACE("(bufferbytes=%ld)\n",dsbd->dwBufferBytes);
512                 TRACE("(lpwfxFormat=%p)\n",dsbd->lpwfxFormat);
513         }
514
515         wfex = dsbd->lpwfxFormat;
516
517         if (wfex)
518                 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
519                    "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
520                    wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
521                    wfex->nAvgBytesPerSec, wfex->nBlockAlign,
522                    wfex->wBitsPerSample, wfex->cbSize);
523
524         if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
525                 hres = PrimaryBuffer_Create(This, (PrimaryBufferImpl**)ppdsb, dsbd);
526                 if (hres != DS_OK)
527                         WARN("PrimaryBuffer_Create failed\n");
528         } else {
529                 hres = SecondaryBuffer_Create(This, (IDirectSoundBufferImpl**)ppdsb, dsbd);
530                 if (hres != DS_OK)
531                         WARN("SecondaryBuffer_Create failed\n");
532         }
533
534         return hres;
535 }
536
537 static HRESULT WINAPI IDirectSoundImpl_DuplicateSoundBuffer(
538         LPDIRECTSOUND8 iface,LPDIRECTSOUNDBUFFER8 pdsb,LPLPDIRECTSOUNDBUFFER8 ppdsb
539 ) {
540         ICOM_THIS(IDirectSoundImpl,iface);
541         IDirectSoundBufferImpl* ipdsb=(IDirectSoundBufferImpl*)pdsb;
542         IDirectSoundBufferImpl* dsb;
543         TRACE("(%p,%p,%p)\n",This,pdsb,ppdsb);
544
545         if (This == NULL) {
546                 WARN("invalid parameter: This == NULL\n");
547                 return DSERR_INVALIDPARAM;
548         }
549
550         if (pdsb == NULL) {
551                 WARN("invalid parameter: pdsb == NULL\n");
552                 return DSERR_INVALIDPARAM;
553         }
554
555         if (ppdsb == NULL) {
556                 WARN("invalid parameter: ppdsb == NULL\n");
557                 return DSERR_INVALIDPARAM;
558         }
559
560         if (ipdsb->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) {
561                 ERR("trying to duplicate primary buffer\n");
562                 *ppdsb = NULL;
563                 return DSERR_INVALIDCALL;
564         }
565
566         if (ipdsb->hwbuf) {
567                 FIXME("need to duplicate hardware buffer\n");
568                 *ppdsb = NULL;
569                 return DSERR_INVALIDCALL;
570         }
571
572         dsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
573
574         if (dsb == NULL) {
575                 WARN("out of memory\n");
576                 *ppdsb = NULL;
577                 return DSERR_OUTOFMEMORY;
578         }
579
580         IDirectSoundBuffer8_AddRef(pdsb);
581         memcpy(dsb, ipdsb, sizeof(IDirectSoundBufferImpl));
582         dsb->ref = 1;
583         dsb->state = STATE_STOPPED;
584         dsb->playpos = 0;
585         dsb->buf_mixpos = 0;
586         dsb->dsound = This;
587         dsb->parent = ipdsb;
588         dsb->hwbuf = NULL;
589         if (ipdsb->ds3db != NULL) {
590                 HRESULT hres;
591                 hres = IDirectSound3DBufferImpl_Create(dsb, &(dsb->ds3db));
592                 if (hres != DS_OK) {
593                         WARN("IDirectSound3DBufferImpl_Create failed\n");
594                 } else {
595                         IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)(dsb->ds3db));
596                         dsb->dsbd.dwFlags |= DSBCAPS_CTRL3D;
597                         memcpy(&(dsb->ds3db->ds3db),  &(ipdsb->ds3db->ds3db), sizeof(DS3DBUFFER));
598                 }
599         } else {
600                 dsb->ds3db = NULL;
601         }
602         dsb->iks = NULL; /* FIXME? */
603         memcpy(&(dsb->wfx), &(ipdsb->wfx), sizeof(dsb->wfx));
604         InitializeCriticalSection(&(dsb->lock));
605         /* register buffer */
606         RtlAcquireResourceExclusive(&(This->lock), TRUE);
607         {
608                 IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1));
609                 if (newbuffers) {
610                         This->buffers = newbuffers;
611                         This->buffers[This->nrofbuffers] = dsb;
612                         This->nrofbuffers++;
613                         TRACE("buffer count is now %d\n", This->nrofbuffers);
614                 } else {
615                         ERR("out of memory for buffer list! Current buffer count is %d\n", This->nrofbuffers);
616                         IDirectSoundBuffer8_Release(pdsb);
617                         DeleteCriticalSection(&(dsb->lock));
618                         RtlReleaseResource(&(This->lock));
619                         HeapFree(GetProcessHeap(),0,dsb);
620                         *ppdsb = 0;
621                         return DSERR_OUTOFMEMORY;
622                 }
623         }
624         RtlReleaseResource(&(This->lock));
625         IDirectSound_AddRef(iface);
626         *ppdsb = (LPDIRECTSOUNDBUFFER8)dsb;
627         return DS_OK;
628 }
629
630
631 static HRESULT WINAPI IDirectSoundImpl_GetCaps(LPDIRECTSOUND8 iface,LPDSCAPS lpDSCaps) {
632         ICOM_THIS(IDirectSoundImpl,iface);
633         TRACE("(%p,%p)\n",This,lpDSCaps);
634
635         if (This == NULL) {
636                 WARN("invalid parameter: This == NULL\n");
637                 return DSERR_INVALIDPARAM;
638         }
639
640         if (lpDSCaps == NULL) {
641                 WARN("invalid parameter: lpDSCaps = NULL\n");
642                 return DSERR_INVALIDPARAM;
643         }
644
645         /* check is there is enough room */
646         if (lpDSCaps->dwSize < sizeof(*lpDSCaps)) {
647                 WARN("invalid parameter: lpDSCaps->dwSize = %ld < %d\n",
648                         lpDSCaps->dwSize, sizeof(*lpDSCaps));
649                 return DSERR_INVALIDPARAM;
650         }
651
652         lpDSCaps->dwFlags = This->drvcaps.dwFlags;
653         TRACE("(flags=0x%08lx)\n",lpDSCaps->dwFlags);
654
655         /* FIXME: copy caps from This->drv */
656         lpDSCaps->dwMinSecondarySampleRate              = DSBFREQUENCY_MIN;
657         lpDSCaps->dwMaxSecondarySampleRate              = DSBFREQUENCY_MAX;
658
659         lpDSCaps->dwPrimaryBuffers                      = 1;
660
661         lpDSCaps->dwMaxHwMixingAllBuffers               = 0;
662         lpDSCaps->dwMaxHwMixingStaticBuffers            = 0;
663         lpDSCaps->dwMaxHwMixingStreamingBuffers         = 0;
664
665         lpDSCaps->dwFreeHwMixingAllBuffers              = 0;
666         lpDSCaps->dwFreeHwMixingStaticBuffers           = 0;
667         lpDSCaps->dwFreeHwMixingStreamingBuffers        = 0;
668
669         lpDSCaps->dwMaxHw3DAllBuffers                   = 0;
670         lpDSCaps->dwMaxHw3DStaticBuffers                = 0;
671         lpDSCaps->dwMaxHw3DStreamingBuffers             = 0;
672
673         lpDSCaps->dwFreeHw3DAllBuffers                  = 0;
674         lpDSCaps->dwFreeHw3DStaticBuffers               = 0;
675         lpDSCaps->dwFreeHw3DStreamingBuffers            = 0;
676
677         lpDSCaps->dwTotalHwMemBytes                     = 0;
678
679         lpDSCaps->dwFreeHwMemBytes                      = 0;
680
681         lpDSCaps->dwMaxContigFreeHwMemBytes             = 0;
682
683         lpDSCaps->dwUnlockTransferRateHwBuffers         = 4096; /* But we have none... */
684
685         lpDSCaps->dwPlayCpuOverheadSwBuffers            = 1;    /* 1% */
686
687         return DS_OK;
688 }
689
690 static ULONG WINAPI IDirectSoundImpl_AddRef(LPDIRECTSOUND8 iface) {
691         ICOM_THIS(IDirectSoundImpl,iface);
692         TRACE("(%p) ref was %ld\n", This, This->ref);
693         return ++(This->ref);
694 }
695
696 static ULONG WINAPI IDirectSoundImpl_Release(LPDIRECTSOUND8 iface) {
697         HRESULT hres;
698         ICOM_THIS(IDirectSoundImpl,iface);
699         TRACE("(%p), ref was %ld\n",This,This->ref);
700         if (!--(This->ref)) {
701                 UINT i;
702
703                 timeKillEvent(This->timerID);
704                 timeEndPeriod(DS_TIME_RES);
705
706                 if (This->buffers) {
707                         for( i=0;i<This->nrofbuffers;i++)
708                                 IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)This->buffers[i]);
709                 }
710
711                 hres = DSOUND_PrimaryDestroy(This);
712                 if (hres != DS_OK)
713                         WARN("DSOUND_PrimaryDestroy failed\n");
714
715                 RtlDeleteResource(&This->lock);
716                 DeleteCriticalSection(&This->mixlock);
717                 if (This->driver) {
718                         IDsDriver_Close(This->driver);
719                 }
720                 if (This->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN) {
721                         waveOutClose(This->hwo);
722                 }
723                 if (This->driver)
724                         IDsDriver_Release(This->driver);
725
726                 HeapFree(GetProcessHeap(),0,This);
727                 dsound = NULL;
728                 return 0;
729         }
730         return This->ref;
731 }
732
733 static HRESULT WINAPI IDirectSoundImpl_SetSpeakerConfig(
734         LPDIRECTSOUND8 iface,DWORD config
735 ) {
736         ICOM_THIS(IDirectSoundImpl,iface);
737         FIXME("(%p,0x%08lx):stub\n",This,config);
738         return DS_OK;
739 }
740
741 static HRESULT WINAPI IDirectSoundImpl_QueryInterface(
742         LPDIRECTSOUND8 iface,REFIID riid,LPVOID *ppobj
743 ) {
744         ICOM_THIS(IDirectSoundImpl,iface);
745
746         if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
747                 ERR("app requested IDirectSound3DListener on dsound object\n");
748                 *ppobj = NULL;
749                 return E_FAIL;
750         }
751
752         FIXME("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
753         return E_NOINTERFACE;
754 }
755
756 static HRESULT WINAPI IDirectSoundImpl_Compact(
757         LPDIRECTSOUND8 iface)
758 {
759         ICOM_THIS(IDirectSoundImpl,iface);
760         TRACE("(%p)\n", This);
761         return DS_OK;
762 }
763
764 static HRESULT WINAPI IDirectSoundImpl_GetSpeakerConfig(
765         LPDIRECTSOUND8 iface,
766         LPDWORD lpdwSpeakerConfig)
767 {
768         ICOM_THIS(IDirectSoundImpl,iface);
769         TRACE("(%p, %p)\n", This, lpdwSpeakerConfig);
770         *lpdwSpeakerConfig = DSSPEAKER_STEREO | (DSSPEAKER_GEOMETRY_NARROW << 16);
771         return DS_OK;
772 }
773
774 static HRESULT WINAPI IDirectSoundImpl_Initialize(
775         LPDIRECTSOUND8 iface,
776         LPCGUID lpcGuid)
777 {
778         ICOM_THIS(IDirectSoundImpl,iface);
779         TRACE("(%p, %s)\n", This, debugstr_guid(lpcGuid));
780         return DS_OK;
781 }
782
783 static HRESULT WINAPI IDirectSoundImpl_VerifyCertification(
784         LPDIRECTSOUND8 iface,
785         LPDWORD pdwCertified)
786 {
787         ICOM_THIS(IDirectSoundImpl,iface);
788         TRACE("(%p, %p)\n", This, pdwCertified);
789         *pdwCertified = DS_CERTIFIED;
790         return DS_OK;
791 }
792
793 static ICOM_VTABLE(IDirectSound8) dsvt =
794 {
795         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
796         IDirectSoundImpl_QueryInterface,
797         IDirectSoundImpl_AddRef,
798         IDirectSoundImpl_Release,
799         IDirectSoundImpl_CreateSoundBuffer,
800         IDirectSoundImpl_GetCaps,
801         IDirectSoundImpl_DuplicateSoundBuffer,
802         IDirectSoundImpl_SetCooperativeLevel,
803         IDirectSoundImpl_Compact,
804         IDirectSoundImpl_GetSpeakerConfig,
805         IDirectSoundImpl_SetSpeakerConfig,
806         IDirectSoundImpl_Initialize,
807         IDirectSoundImpl_VerifyCertification
808 };
809
810
811 /*******************************************************************************
812  *              DirectSoundCreate (DSOUND.1)
813  *
814  *  Creates and initializes a DirectSound interface.
815  *
816  *  PARAMS
817  *     lpcGUID   [I] Address of the GUID that identifies the sound device.
818  *     ppDS      [O] Address of a variable to receive the interface pointer.
819  *     pUnkOuter [I] Must be NULL.
820  *
821  *  RETURNS
822  *     Success: DS_OK
823  *     Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION, 
824  *              DSERR_NODRIVER, DSERR_OUTOFMEMORY
825  */
826 HRESULT WINAPI DirectSoundCreate8(LPCGUID lpcGUID,LPDIRECTSOUND8 *ppDS,IUnknown *pUnkOuter )
827 {
828         IDirectSoundImpl** ippDS=(IDirectSoundImpl**)ppDS;
829         PIDSDRIVER drv = NULL;
830         unsigned wod, wodn;
831         HRESULT err = DSERR_INVALIDPARAM;
832         GUID devGuid;
833         BOOLEAN found = FALSE;
834
835         TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID),ippDS,pUnkOuter);
836         
837         if (ippDS == NULL) {
838                 WARN("invalid parameter: ippDS == NULL\n");
839                 return DSERR_INVALIDPARAM;
840         }
841
842         /* Get dsound configuration */
843         setup_dsound_options();
844
845         /* Default device? */
846         if (!lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL))
847                 lpcGUID = &DSDEVID_DefaultPlayback;
848
849         if (GetDeviceID(lpcGUID, &devGuid) != DS_OK) {
850                 WARN("invalid parameter: lpcGUID\n");
851                 *ippDS = NULL;
852                 return DSERR_INVALIDPARAM;
853         }
854
855         if (dsound) {
856                 if (IsEqualGUID(&devGuid, &dsound->guid) ) {
857                         ERR("dsound already opened\n");
858                         IDirectSound_AddRef((LPDIRECTSOUND)dsound);
859                         *ippDS = dsound;
860                         return DS_OK;
861                 } else {
862                         ERR("different dsound already opened\n");
863                 }
864         }
865
866         /* Enumerate WINMM audio devices and find the one we want */
867         wodn = waveOutGetNumDevs();
868         if (!wodn) {
869                 WARN("no driver\n");
870                 *ippDS = NULL;
871                 return DSERR_NODRIVER;
872         }
873
874         TRACE(" expecting GUID %s.\n", debugstr_guid(&devGuid));
875         
876         for (wod=0; wod<wodn; wod++) {
877                 GUID guid;
878                 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDGUID,(DWORD)(&guid),0));
879                 if (err != DS_OK) {
880                         WARN("waveOutMessage failed; err=%lx\n",err);
881                         *ippDS = NULL;
882                         return err;
883                 }
884                 TRACE("got GUID %s for wod %d.\n", debugstr_guid(&guid), wod);
885                 if (IsEqualGUID( &devGuid, &guid) ) {
886                         err = DS_OK;
887                         found = TRUE;
888                         break;
889                 }
890         }
891
892         if (err != DS_OK) {
893                 WARN("invalid parameter\n");
894                 *ippDS = NULL;
895                 return DSERR_INVALIDPARAM;
896         }
897
898         if (found == FALSE) {
899                 WARN("No device found matching given ID - trying with default one !\n");
900                 wod = ds_default_playback;      
901         }
902         
903         /* DRV_QUERYDSOUNDIFACE is a "Wine extension" to get the DSound interface */
904         waveOutMessage((HWAVEOUT)wod, DRV_QUERYDSOUNDIFACE, (DWORD)&drv, 0);
905
906         /* Disable the direct sound driver to force emulation if requested. */
907         if (ds_hw_accel == DS_HW_ACCEL_EMULATION)
908             drv = NULL;
909         
910         /* Allocate memory */
911         *ippDS = (IDirectSoundImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundImpl));
912         if (*ippDS == NULL) {
913                 WARN("out of memory\n");
914                 return DSERR_OUTOFMEMORY;
915         }
916
917         (*ippDS)->lpVtbl        = &dsvt;
918         (*ippDS)->ref           = 1;
919
920         (*ippDS)->driver        = drv;
921         (*ippDS)->priolevel     = DSSCL_NORMAL;
922         (*ippDS)->fraglen       = 0;
923         (*ippDS)->hwbuf         = NULL;
924         (*ippDS)->buffer        = NULL;
925         (*ippDS)->buflen        = 0;
926         (*ippDS)->writelead     = 0;
927         (*ippDS)->state         = STATE_STOPPED;
928         (*ippDS)->nrofbuffers   = 0;
929         (*ippDS)->buffers       = NULL;
930         (*ippDS)->listener      = NULL;
931
932         (*ippDS)->prebuf        = ds_snd_queue_max;
933         (*ippDS)->guid          = devGuid;
934
935         /* Get driver description */
936         if (drv) {
937                 err = IDsDriver_GetDriverDesc(drv,&((*ippDS)->drvdesc));
938                 if (err != DS_OK) {
939                         WARN("IDsDriver_GetDriverDesc failed\n");
940                         HeapFree(GetProcessHeap(),0,*ippDS);
941                         *ippDS = NULL;
942                         return err;
943                 }
944         } else {
945                 /* if no DirectSound interface available, use WINMM API instead */
946                 (*ippDS)->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT;
947         }
948
949         (*ippDS)->drvdesc.dnDevNode = wod;
950
951         /* Set default wave format (may need it for waveOutOpen) */
952         (*ippDS)->wfx.wFormatTag        = WAVE_FORMAT_PCM;
953         /* We rely on the sound driver to return the actual sound format of 
954          * the device if it does not support 22050x8x2 and is given the 
955          * WAVE_DIRECTSOUND flag.
956          */
957         (*ippDS)->wfx.nSamplesPerSec = 22050;
958         (*ippDS)->wfx.wBitsPerSample = 8;
959         (*ippDS)->wfx.nChannels = 2;
960         (*ippDS)->wfx.nBlockAlign = (*ippDS)->wfx.wBitsPerSample * (*ippDS)->wfx.nChannels / 8;
961         (*ippDS)->wfx.nAvgBytesPerSec = (*ippDS)->wfx.nSamplesPerSec * (*ippDS)->wfx.nBlockAlign;
962         (*ippDS)->wfx.cbSize = 0;
963
964         /* If the driver requests being opened through MMSYSTEM
965          * (which is recommended by the DDK), it is supposed to happen
966          * before the DirectSound interface is opened */
967         if ((*ippDS)->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
968         {
969                 DWORD flags = CALLBACK_FUNCTION;
970
971                 /* disable direct sound if requested */
972                 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
973                     flags |= WAVE_DIRECTSOUND;
974
975                 err = mmErr(waveOutOpen(&((*ippDS)->hwo),
976                                           (*ippDS)->drvdesc.dnDevNode, &((*ippDS)->wfx),
977                                           (DWORD)DSOUND_callback, (DWORD)(*ippDS),
978                                           flags));
979                 if (err != DS_OK) {
980                         WARN("waveOutOpen failed\n");
981                         HeapFree(GetProcessHeap(),0,*ippDS);
982                         *ippDS = NULL;
983                         return err;
984                 }
985         }
986
987         if (drv) {
988                 err = IDsDriver_Open(drv);
989                 if (err != DS_OK) {
990                         WARN("IDsDriver_Open failed\n");
991                         HeapFree(GetProcessHeap(),0,*ippDS);
992                         *ippDS = NULL;
993                         return err;
994                 }
995
996                 /* the driver is now open, so it's now allowed to call GetCaps */
997                 err = IDsDriver_GetCaps(drv,&((*ippDS)->drvcaps));
998                 if (err != DS_OK) {
999                         WARN("IDsDriver_GetCaps failed\n");
1000                         HeapFree(GetProcessHeap(),0,*ippDS);
1001                         *ippDS = NULL;
1002                         return err;
1003                 }
1004         } else {
1005                 WAVEOUTCAPSA woc;
1006                 err = mmErr(waveOutGetDevCapsA((*ippDS)->drvdesc.dnDevNode, &woc, sizeof(woc)));
1007                 if (err != DS_OK) {
1008                         WARN("waveOutGetDevCaps failed\n");
1009                         HeapFree(GetProcessHeap(),0,*ippDS);
1010                         *ippDS = NULL;
1011                         return err;
1012                 }
1013                 (*ippDS)->drvcaps.dwFlags = 0;
1014                 if ((woc.dwFormats & WAVE_FORMAT_1M08) ||
1015                     (woc.dwFormats & WAVE_FORMAT_2M08) ||
1016                     (woc.dwFormats & WAVE_FORMAT_4M08) ||
1017                     (woc.dwFormats & WAVE_FORMAT_48M08) ||
1018                     (woc.dwFormats & WAVE_FORMAT_96M08)) {
1019                         (*ippDS)->drvcaps.dwFlags |= DSCAPS_PRIMARY8BIT;
1020                         (*ippDS)->drvcaps.dwFlags |= DSCAPS_PRIMARYMONO;
1021                 }
1022                 if ((woc.dwFormats & WAVE_FORMAT_1M16) ||
1023                     (woc.dwFormats & WAVE_FORMAT_2M16) ||
1024                     (woc.dwFormats & WAVE_FORMAT_4M16) ||
1025                     (woc.dwFormats & WAVE_FORMAT_48M16) ||
1026                     (woc.dwFormats & WAVE_FORMAT_96M16)) {
1027                         (*ippDS)->drvcaps.dwFlags |= DSCAPS_PRIMARY16BIT;
1028                         (*ippDS)->drvcaps.dwFlags |= DSCAPS_PRIMARYMONO;
1029                 }
1030                 if ((woc.dwFormats & WAVE_FORMAT_1S08) ||
1031                     (woc.dwFormats & WAVE_FORMAT_2S08) ||
1032                     (woc.dwFormats & WAVE_FORMAT_4S08) ||
1033                     (woc.dwFormats & WAVE_FORMAT_48S08) ||
1034                     (woc.dwFormats & WAVE_FORMAT_96S08)) {
1035                         (*ippDS)->drvcaps.dwFlags |= DSCAPS_PRIMARY8BIT;
1036                         (*ippDS)->drvcaps.dwFlags |= DSCAPS_PRIMARYSTEREO;
1037                 }
1038                 if ((woc.dwFormats & WAVE_FORMAT_1S16) ||
1039                     (woc.dwFormats & WAVE_FORMAT_2S16) ||
1040                     (woc.dwFormats & WAVE_FORMAT_4S16) ||
1041                     (woc.dwFormats & WAVE_FORMAT_48S16) ||
1042                     (woc.dwFormats & WAVE_FORMAT_96S16)) {
1043                         (*ippDS)->drvcaps.dwFlags |= DSCAPS_PRIMARY16BIT;
1044                         (*ippDS)->drvcaps.dwFlags |= DSCAPS_PRIMARYSTEREO;
1045                 }
1046                 if (ds_emuldriver)
1047                     (*ippDS)->drvcaps.dwFlags |= DSCAPS_EMULDRIVER;
1048         }
1049
1050         DSOUND_RecalcVolPan(&((*ippDS)->volpan));
1051
1052         InitializeCriticalSection(&((*ippDS)->mixlock));
1053         RtlInitializeResource(&((*ippDS)->lock));
1054
1055         if (!dsound) {
1056                 HRESULT hres;
1057                 dsound = (*ippDS);
1058                 hres = DSOUND_PrimaryCreate(dsound);
1059                 if (hres != DS_OK) {
1060                         WARN("DSOUND_PrimaryCreate failed\n");
1061                         return hres;
1062                 }
1063                 timeBeginPeriod(DS_TIME_RES);
1064                 dsound->timerID = timeSetEvent(DS_TIME_DEL, DS_TIME_RES, DSOUND_timer,
1065                                                (DWORD)dsound, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
1066         }
1067         return DS_OK;
1068 }
1069
1070
1071 /*******************************************************************************
1072  * DirectSound ClassFactory
1073  */
1074 typedef struct
1075 {
1076     /* IUnknown fields */
1077     ICOM_VFIELD(IClassFactory);
1078     DWORD                       ref;
1079 } IClassFactoryImpl;
1080
1081 static HRESULT WINAPI
1082 DSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
1083         ICOM_THIS(IClassFactoryImpl,iface);
1084
1085         FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1086         return E_NOINTERFACE;
1087 }
1088
1089 static ULONG WINAPI
1090 DSCF_AddRef(LPCLASSFACTORY iface) {
1091         ICOM_THIS(IClassFactoryImpl,iface);
1092         TRACE("(%p) ref was %ld\n", This, This->ref);
1093         return ++(This->ref);
1094 }
1095
1096 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface) {
1097         ICOM_THIS(IClassFactoryImpl,iface);
1098         /* static class, won't be  freed */
1099         TRACE("(%p) ref was %ld\n", This, This->ref);
1100         return --(This->ref);
1101 }
1102
1103 static HRESULT WINAPI DSCF_CreateInstance(
1104         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
1105 ) {
1106         ICOM_THIS(IClassFactoryImpl,iface);
1107
1108         TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1109         if ( IsEqualGUID( &IID_IDirectSound, riid ) ||
1110              IsEqualGUID( &IID_IDirectSound8, riid ) ) {
1111                 /* FIXME: reuse already created dsound if present? */
1112                 return DirectSoundCreate8(0,(LPDIRECTSOUND8*)ppobj,pOuter);
1113         }
1114         if ( IsEqualGUID( &IID_IDirectSoundCapture, riid ) ||
1115              IsEqualGUID( &IID_IDirectSoundCapture8, riid ) ) {
1116                 return DirectSoundCaptureCreate8(0,(LPDIRECTSOUNDCAPTURE8*)ppobj,pOuter);
1117         }
1118         if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1119                 return IKsPropertySetImpl_Create(0,(IKsPropertySetImpl**)ppobj);
1120         }
1121
1122         FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);    
1123         return E_NOINTERFACE;
1124 }
1125
1126 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
1127         ICOM_THIS(IClassFactoryImpl,iface);
1128         FIXME("(%p)->(%d),stub!\n",This,dolock);
1129         return S_OK;
1130 }
1131
1132 static ICOM_VTABLE(IClassFactory) DSCF_Vtbl = {
1133         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1134         DSCF_QueryInterface,
1135         DSCF_AddRef,
1136         DSCF_Release,
1137         DSCF_CreateInstance,
1138         DSCF_LockServer
1139 };
1140 static IClassFactoryImpl DSOUND_CF = {&DSCF_Vtbl, 1 };
1141
1142 /*******************************************************************************
1143  * DllGetClassObject [DSOUND.5]
1144  * Retrieves class object from a DLL object
1145  *
1146  * NOTES
1147  *    Docs say returns STDAPI
1148  *
1149  * PARAMS
1150  *    rclsid [I] CLSID for the class object
1151  *    riid   [I] Reference to identifier of interface for class object
1152  *    ppv    [O] Address of variable to receive interface pointer for riid
1153  *
1154  * RETURNS
1155  *    Success: S_OK
1156  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
1157  *             E_UNEXPECTED
1158  */
1159 DWORD WINAPI DSOUND_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
1160 {
1161     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1162     if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
1163         *ppv = (LPVOID)&DSOUND_CF;
1164         IClassFactory_AddRef((IClassFactory*)*ppv);
1165         return S_OK;
1166     }
1167
1168     FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1169     return CLASS_E_CLASSNOTAVAILABLE;
1170 }
1171
1172
1173 /*******************************************************************************
1174  * DllCanUnloadNow [DSOUND.4]  
1175  * Determines whether the DLL is in use.
1176  *
1177  * RETURNS
1178  *    Success: S_OK
1179  *    Failure: S_FALSE
1180  */
1181 DWORD WINAPI DSOUND_DllCanUnloadNow(void)
1182 {
1183     FIXME("(void): stub\n");
1184     return S_FALSE;
1185 }