Keep track of per-column information inside the listview.
[wine] / dlls / dsound / sound3d.c
1 /*                      DirectSound
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998 Rob Riggs
5  * Copyright 2000-2001 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 <assert.h>
41 #include <stdio.h>
42 #include <sys/types.h>
43 #include <sys/fcntl.h>
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif
47 #include <stdlib.h>
48 #include <string.h>
49 #include <math.h>       /* Insomnia - pow() function */
50
51 #include "windef.h"
52 #include "winbase.h"
53 #include "wingdi.h"
54 #include "winuser.h"
55 #include "winerror.h"
56 #include "mmsystem.h"
57 #include "winternl.h"
58 #include "mmddk.h"
59 #include "wine/windef16.h"
60 #include "wine/debug.h"
61 #include "dsound.h"
62 #include "dsdriver.h"
63 #include "dsound_private.h"
64
65 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
66
67
68 /*******************************************************************************
69  *              IDirectSound3DBuffer
70  */
71
72 /* IUnknown methods */
73 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(
74         LPDIRECTSOUND3DBUFFER iface, REFIID riid, LPVOID *ppobj)
75 {
76         ICOM_THIS(IDirectSound3DBufferImpl,iface);
77
78         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
79         return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
80 }
81
82 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(LPDIRECTSOUND3DBUFFER iface)
83 {
84         ICOM_THIS(IDirectSound3DBufferImpl,iface);
85         ULONG ulReturn;
86
87         TRACE("(%p) ref was %ld\n", This, This->ref);
88         ulReturn = InterlockedIncrement(&This->ref);
89         if (ulReturn == 1)
90                 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This->dsb);
91         return ulReturn;
92 }
93
94 static ULONG WINAPI IDirectSound3DBufferImpl_Release(LPDIRECTSOUND3DBUFFER iface)
95 {
96         ICOM_THIS(IDirectSound3DBufferImpl,iface);
97         ULONG ulReturn;
98
99         TRACE("(%p) ref was %ld\n", This, This->ref);
100
101         ulReturn = InterlockedDecrement(&This->ref);
102         if(ulReturn)
103                 return ulReturn;
104
105         if (This->dsb) {
106                 BOOL std = (This->dsb->dsbd.dwFlags & DSBCAPS_CTRL3D);
107
108                 IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
109
110                 if (std)
111                         return 0; /* leave it to IDirectSoundBufferImpl_Release */
112         }
113
114         if (This->dsb->ds3db == This) This->dsb->ds3db = NULL;
115
116         DeleteCriticalSection(&This->lock);
117
118         HeapFree(GetProcessHeap(),0,This);
119
120         return 0;
121 }
122
123 /* IDirectSound3DBuffer methods */
124 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(
125         LPDIRECTSOUND3DBUFFER iface,
126         LPDS3DBUFFER lpDs3dBuffer)
127 {
128         FIXME("stub\n");
129         return DS_OK;
130 }
131
132 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(
133         LPDIRECTSOUND3DBUFFER iface,
134         LPDWORD lpdwInsideConeAngle,
135         LPDWORD lpdwOutsideConeAngle)
136 {
137         FIXME("stub\n");
138         return DS_OK;
139 }
140
141 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(
142         LPDIRECTSOUND3DBUFFER iface,
143         LPD3DVECTOR lpvConeOrientation)
144 {
145         FIXME("stub\n");
146         return DS_OK;
147 }
148
149 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(
150         LPDIRECTSOUND3DBUFFER iface,
151         LPLONG lplConeOutsideVolume)
152 {
153         FIXME("stub\n");
154         return DS_OK;
155 }
156
157 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(
158         LPDIRECTSOUND3DBUFFER iface,
159         LPD3DVALUE lpfMaxDistance)
160 {
161         FIXME("stub\n");
162         return DS_OK;
163 }
164
165 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(
166         LPDIRECTSOUND3DBUFFER iface,
167         LPD3DVALUE lpfMinDistance)
168 {
169         FIXME("stub\n");
170         return DS_OK;
171 }
172
173 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(
174         LPDIRECTSOUND3DBUFFER iface,
175         LPDWORD lpdwMode)
176 {
177         FIXME("stub\n");
178         return DS_OK;
179 }
180
181 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(
182         LPDIRECTSOUND3DBUFFER iface,
183         LPD3DVECTOR lpvPosition)
184 {
185         FIXME("stub\n");
186         return DS_OK;
187 }
188
189 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(
190         LPDIRECTSOUND3DBUFFER iface,
191         LPD3DVECTOR lpvVelocity)
192 {
193         FIXME("stub\n");
194         return DS_OK;
195 }
196
197 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(
198         LPDIRECTSOUND3DBUFFER iface,
199         LPCDS3DBUFFER lpcDs3dBuffer,
200         DWORD dwApply)
201 {
202         FIXME("stub\n");
203         return DS_OK;
204 }
205
206 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(
207         LPDIRECTSOUND3DBUFFER iface,
208         DWORD dwInsideConeAngle,
209         DWORD dwOutsideConeAngle,
210         DWORD dwApply)
211 {
212         FIXME("stub\n");
213         return DS_OK;
214 }
215
216 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(
217         LPDIRECTSOUND3DBUFFER iface,
218         D3DVALUE x, D3DVALUE y, D3DVALUE z,
219         DWORD dwApply)
220 {
221         FIXME("stub\n");
222         return DS_OK;
223 }
224
225 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(
226         LPDIRECTSOUND3DBUFFER iface,
227         LONG lConeOutsideVolume,
228         DWORD dwApply)
229 {
230         FIXME("stub\n");
231         return DS_OK;
232 }
233
234 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(
235         LPDIRECTSOUND3DBUFFER iface,
236         D3DVALUE fMaxDistance,
237         DWORD dwApply)
238 {
239         FIXME("stub\n");
240         return DS_OK;
241 }
242
243 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(
244         LPDIRECTSOUND3DBUFFER iface,
245         D3DVALUE fMinDistance,
246         DWORD dwApply)
247 {
248         FIXME("stub\n");
249         return DS_OK;
250 }
251
252 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(
253         LPDIRECTSOUND3DBUFFER iface,
254         DWORD dwMode,
255         DWORD dwApply)
256 {
257         ICOM_THIS(IDirectSound3DBufferImpl,iface);
258         TRACE("mode = %lx\n", dwMode);
259         This->ds3db.dwMode = dwMode;
260         return DS_OK;
261 }
262
263 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(
264         LPDIRECTSOUND3DBUFFER iface,
265         D3DVALUE x, D3DVALUE y, D3DVALUE z,
266         DWORD dwApply)
267 {
268         FIXME("stub\n");
269         return DS_OK;
270 }
271
272 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(
273         LPDIRECTSOUND3DBUFFER iface,
274         D3DVALUE x, D3DVALUE y, D3DVALUE z,
275         DWORD dwApply)
276 {
277         FIXME("stub\n");
278         return DS_OK;
279 }
280
281 static ICOM_VTABLE(IDirectSound3DBuffer) ds3dbvt =
282 {
283         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
284         /* IUnknown methods */
285         IDirectSound3DBufferImpl_QueryInterface,
286         IDirectSound3DBufferImpl_AddRef,
287         IDirectSound3DBufferImpl_Release,
288         /* IDirectSound3DBuffer methods */
289         IDirectSound3DBufferImpl_GetAllParameters,
290         IDirectSound3DBufferImpl_GetConeAngles,
291         IDirectSound3DBufferImpl_GetConeOrientation,
292         IDirectSound3DBufferImpl_GetConeOutsideVolume,
293         IDirectSound3DBufferImpl_GetMaxDistance,
294         IDirectSound3DBufferImpl_GetMinDistance,
295         IDirectSound3DBufferImpl_GetMode,
296         IDirectSound3DBufferImpl_GetPosition,
297         IDirectSound3DBufferImpl_GetVelocity,
298         IDirectSound3DBufferImpl_SetAllParameters,
299         IDirectSound3DBufferImpl_SetConeAngles,
300         IDirectSound3DBufferImpl_SetConeOrientation,
301         IDirectSound3DBufferImpl_SetConeOutsideVolume,
302         IDirectSound3DBufferImpl_SetMaxDistance,
303         IDirectSound3DBufferImpl_SetMinDistance,
304         IDirectSound3DBufferImpl_SetMode,
305         IDirectSound3DBufferImpl_SetPosition,
306         IDirectSound3DBufferImpl_SetVelocity,
307 };
308
309 HRESULT WINAPI IDirectSound3DBufferImpl_Create(
310         IDirectSoundBufferImpl *This,
311         IDirectSound3DBufferImpl **pds3db)
312 {
313         IDirectSound3DBufferImpl *ds3db;
314
315         ds3db = (IDirectSound3DBufferImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*ds3db));
316         ds3db->ref = 0;
317         ds3db->dsb = This;
318         ICOM_VTBL(ds3db) = &ds3dbvt;
319         InitializeCriticalSection(&ds3db->lock);
320
321         ds3db->ds3db.dwSize = sizeof(DS3DBUFFER);
322         ds3db->ds3db.vPosition.u1.x = 0.0;
323         ds3db->ds3db.vPosition.u2.y = 0.0;
324         ds3db->ds3db.vPosition.u3.z = 0.0;
325         ds3db->ds3db.vVelocity.u1.x = 0.0;
326         ds3db->ds3db.vVelocity.u2.y = 0.0;
327         ds3db->ds3db.vVelocity.u3.z = 0.0;
328         ds3db->ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
329         ds3db->ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
330         ds3db->ds3db.vConeOrientation.u1.x = 0.0;
331         ds3db->ds3db.vConeOrientation.u2.y = 0.0;
332         ds3db->ds3db.vConeOrientation.u3.z = 0.0;
333         ds3db->ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
334         ds3db->ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
335         ds3db->ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
336         ds3db->ds3db.dwMode = DS3DMODE_NORMAL;
337
338         *pds3db = ds3db;
339         return S_OK;
340 }
341
342 /*******************************************************************************
343  *            IDirectSound3DListener
344  */
345
346 /* IUnknown methods */
347 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
348         LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
349 {
350         ICOM_THIS(IDirectSound3DListenerImpl,iface);
351
352         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
353         return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
354 }
355
356 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
357 {
358         ICOM_THIS(IDirectSound3DListenerImpl,iface);
359         return InterlockedIncrement(&This->ref);
360 }
361
362 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
363 {
364         ICOM_THIS(IDirectSound3DListenerImpl,iface);
365         ULONG ulReturn;
366
367         TRACE("(%p) ref was %ld\n", This, This->ref);
368
369         ulReturn = InterlockedDecrement(&This->ref);
370
371         /* Free all resources */
372         if( ulReturn == 0 ) {
373                 if(This->dsb)
374                         IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
375                 DeleteCriticalSection(&This->lock);
376                 HeapFree(GetProcessHeap(),0,This);
377         }
378
379         return ulReturn;
380 }
381
382 /* IDirectSound3DListener methods */
383 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
384         LPDIRECTSOUND3DLISTENER iface,
385         LPDS3DLISTENER lpDS3DL)
386 {
387         FIXME("stub\n");
388         return DS_OK;
389 }
390
391 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
392         LPDIRECTSOUND3DLISTENER iface,
393         LPD3DVALUE lpfDistanceFactor)
394 {
395         FIXME("stub\n");
396         return DS_OK;
397 }
398
399 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
400         LPDIRECTSOUND3DLISTENER iface,
401         LPD3DVALUE lpfDopplerFactor)
402 {
403         FIXME("stub\n");
404         return DS_OK;
405 }
406
407 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
408         LPDIRECTSOUND3DLISTENER iface,
409         LPD3DVECTOR lpvOrientFront,
410         LPD3DVECTOR lpvOrientTop)
411 {
412         FIXME("stub\n");
413         return DS_OK;
414 }
415
416 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
417         LPDIRECTSOUND3DLISTENER iface,
418         LPD3DVECTOR lpvPosition)
419 {
420         FIXME("stub\n");
421         return DS_OK;
422 }
423
424 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
425         LPDIRECTSOUND3DLISTENER iface,
426         LPD3DVALUE lpfRolloffFactor)
427 {
428         FIXME("stub\n");
429         return DS_OK;
430 }
431
432 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
433         LPDIRECTSOUND3DLISTENER iface,
434         LPD3DVECTOR lpvVelocity)
435 {
436         FIXME("stub\n");
437         return DS_OK;
438 }
439
440 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
441         LPDIRECTSOUND3DLISTENER iface,
442         LPCDS3DLISTENER lpcDS3DL,
443         DWORD dwApply)
444 {
445         FIXME("stub\n");
446         return DS_OK;
447 }
448
449 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
450         LPDIRECTSOUND3DLISTENER iface,
451         D3DVALUE fDistanceFactor,
452         DWORD dwApply)
453 {
454         FIXME("stub\n");
455         return DS_OK;
456 }
457
458 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
459         LPDIRECTSOUND3DLISTENER iface,
460         D3DVALUE fDopplerFactor,
461         DWORD dwApply)
462 {
463         FIXME("stub\n");
464         return DS_OK;
465 }
466
467 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
468         LPDIRECTSOUND3DLISTENER iface,
469         D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
470         D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
471         DWORD dwApply)
472 {
473         FIXME("stub\n");
474         return DS_OK;
475 }
476
477 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
478         LPDIRECTSOUND3DLISTENER iface,
479         D3DVALUE x, D3DVALUE y, D3DVALUE z,
480         DWORD dwApply)
481 {
482         FIXME("stub\n");
483         return DS_OK;
484 }
485
486 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
487         LPDIRECTSOUND3DLISTENER iface,
488         D3DVALUE fRolloffFactor,
489         DWORD dwApply)
490 {
491         FIXME("stub\n");
492         return DS_OK;
493 }
494
495 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
496         LPDIRECTSOUND3DLISTENER iface,
497         D3DVALUE x, D3DVALUE y, D3DVALUE z,
498         DWORD dwApply)
499 {
500         FIXME("stub\n");
501         return DS_OK;
502 }
503
504 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
505         LPDIRECTSOUND3DLISTENER iface)
506
507 {
508         FIXME("stub\n");
509         return DS_OK;
510 }
511
512 static ICOM_VTABLE(IDirectSound3DListener) ds3dlvt =
513 {
514         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
515         /* IUnknown methods */
516         IDirectSound3DListenerImpl_QueryInterface,
517         IDirectSound3DListenerImpl_AddRef,
518         IDirectSound3DListenerImpl_Release,
519         /* IDirectSound3DListener methods */
520         IDirectSound3DListenerImpl_GetAllParameter,
521         IDirectSound3DListenerImpl_GetDistanceFactor,
522         IDirectSound3DListenerImpl_GetDopplerFactor,
523         IDirectSound3DListenerImpl_GetOrientation,
524         IDirectSound3DListenerImpl_GetPosition,
525         IDirectSound3DListenerImpl_GetRolloffFactor,
526         IDirectSound3DListenerImpl_GetVelocity,
527         IDirectSound3DListenerImpl_SetAllParameters,
528         IDirectSound3DListenerImpl_SetDistanceFactor,
529         IDirectSound3DListenerImpl_SetDopplerFactor,
530         IDirectSound3DListenerImpl_SetOrientation,
531         IDirectSound3DListenerImpl_SetPosition,
532         IDirectSound3DListenerImpl_SetRolloffFactor,
533         IDirectSound3DListenerImpl_SetVelocity,
534         IDirectSound3DListenerImpl_CommitDeferredSettings,
535 };
536
537 HRESULT WINAPI IDirectSound3DListenerImpl_Create(
538         PrimaryBufferImpl *This,
539         IDirectSound3DListenerImpl **pdsl)
540 {
541         IDirectSound3DListenerImpl *dsl;
542
543         dsl = (IDirectSound3DListenerImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*dsl));
544         dsl->ref = 1;
545         ICOM_VTBL(dsl) = &ds3dlvt;
546
547         dsl->ds3dl.dwSize = sizeof(DS3DLISTENER);
548         dsl->ds3dl.vPosition.u1.x = 0.0;
549         dsl->ds3dl.vPosition.u2.y = 0.0;
550         dsl->ds3dl.vPosition.u3.z = 0.0;
551         dsl->ds3dl.vVelocity.u1.x = 0.0;
552         dsl->ds3dl.vVelocity.u2.y = 0.0;
553         dsl->ds3dl.vVelocity.u3.z = 0.0;
554         dsl->ds3dl.vOrientFront.u1.x = 0.0;
555         dsl->ds3dl.vOrientFront.u2.y = 0.0;
556         dsl->ds3dl.vOrientFront.u3.z = 1.0;
557         dsl->ds3dl.vOrientTop.u1.x = 0.0;
558         dsl->ds3dl.vOrientTop.u2.y = 1.0;
559         dsl->ds3dl.vOrientTop.u3.z = 0.0;
560         dsl->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
561         dsl->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
562
563         InitializeCriticalSection(&dsl->lock);
564
565         dsl->dsb = This;
566         IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This);
567
568         *pdsl = dsl;
569         return S_OK;
570 }