wined3d: Return WINED3DERR_NOTAVAILABLE when cubemapping isn't supported.
[wine/wine-kai.git] / dlls / winealsa.drv / dsoutput.c
blob8d91abf21797055fd77fb443bfc5cb1b380b840c
1 /*
2 * Sample Wine Driver for Advanced Linux Sound System (ALSA)
3 * Based on version <final> of the ALSA API
5 * Copyright 2002 Eric Pouech
6 * 2002 Marco Pietrobono
7 * 2003 Christian Costa : WaveIn support
8 * 2006-2007 Maarten Lankhorst
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 /*======================================================================*
26 * Low level dsound output implementation *
27 *======================================================================*/
29 #include "config.h"
30 #include "wine/port.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <string.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <errno.h>
41 #include <limits.h>
42 #include <fcntl.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 # include <sys/ioctl.h>
45 #endif
46 #ifdef HAVE_SYS_MMAN_H
47 # include <sys/mman.h>
48 #endif
49 #include "windef.h"
50 #include "winbase.h"
51 #include "wingdi.h"
52 #include "winerror.h"
53 #include "winuser.h"
54 #include "mmddk.h"
56 #include "alsa.h"
57 #include "wine/library.h"
58 #include "wine/unicode.h"
59 #include "wine/debug.h"
61 #ifdef HAVE_ALSA
63 WINE_DEFAULT_DEBUG_CHANNEL(dsalsa);
65 typedef struct IDsDriverImpl IDsDriverImpl;
66 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
68 struct IDsDriverImpl
70 /* IUnknown fields */
71 const IDsDriverVtbl *lpVtbl;
72 LONG ref;
74 /* IDsDriverImpl fields */
75 IDsDriverBufferImpl* primary;
76 UINT wDevID;
79 struct IDsDriverBufferImpl
81 const IDsDriverBufferVtbl *lpVtbl;
82 LONG ref;
83 IDsDriverImpl* drv;
85 CRITICAL_SECTION pcm_crst;
86 LPVOID mmap_buffer;
87 DWORD mmap_buflen_bytes;
89 snd_pcm_t *pcm;
90 snd_pcm_hw_params_t *hw_params;
91 snd_pcm_sw_params_t *sw_params;
92 snd_pcm_uframes_t mmap_buflen_frames, mmap_pos, mmap_commitahead;
95 /** Fill buffers, for starting and stopping
96 * Alsa won't start playing until everything is filled up
97 * This also updates mmap_pos
99 * Returns: Amount of periods in use so snd_pcm_avail_update
100 * doesn't have to be called up to 4x in GetPosition()
102 static snd_pcm_uframes_t CommitAll(IDsDriverBufferImpl *This)
104 const snd_pcm_channel_area_t *areas;
105 snd_pcm_uframes_t used;
106 const snd_pcm_uframes_t commitahead = This->mmap_commitahead;
108 used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
109 TRACE("%p needs to commit to %lu, used: %lu\n", This, commitahead, used);
110 if (used < commitahead)
112 snd_pcm_uframes_t done, putin = commitahead - used;
113 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
114 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
115 This->mmap_pos += done;
116 used += done;
117 putin = commitahead - used;
119 if (This->mmap_pos == This->mmap_buflen_frames && (snd_pcm_sframes_t)putin > 0)
121 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
122 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
123 This->mmap_pos += done;
124 used += done;
128 if (This->mmap_pos == This->mmap_buflen_frames)
129 This->mmap_pos = 0;
131 return used;
134 static void CheckXRUN(IDsDriverBufferImpl* This)
136 snd_pcm_state_t state = snd_pcm_state(This->pcm);
137 snd_pcm_sframes_t delay;
138 int err;
140 snd_pcm_hwsync(This->pcm);
141 snd_pcm_delay(This->pcm, &delay);
142 if ( state == SND_PCM_STATE_XRUN )
144 err = snd_pcm_prepare(This->pcm);
145 CommitAll(This);
146 snd_pcm_start(This->pcm);
147 WARN("xrun occurred\n");
148 if ( err < 0 )
149 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
151 else if ( state == SND_PCM_STATE_SUSPENDED )
153 int err = snd_pcm_resume(This->pcm);
154 TRACE("recovery from suspension occurred\n");
155 if (err < 0 && err != -EAGAIN){
156 err = snd_pcm_prepare(This->pcm);
157 if (err < 0)
158 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
160 } else if ( state != SND_PCM_STATE_RUNNING ) {
161 FIXME("Unhandled state: %d\n", state);
166 * Allocate the memory-mapped buffer for direct sound, and set up the
167 * callback.
169 static int DSDB_CreateMMAP(IDsDriverBufferImpl* pdbi)
171 snd_pcm_t *pcm = pdbi->pcm;
172 snd_pcm_format_t format;
173 snd_pcm_uframes_t frames, ofs, avail, psize, boundary;
174 unsigned int channels, bits_per_sample, bits_per_frame;
175 int err, mmap_mode;
176 const snd_pcm_channel_area_t *areas;
177 snd_pcm_hw_params_t *hw_params = pdbi->hw_params;
178 snd_pcm_sw_params_t *sw_params = pdbi->sw_params;
180 mmap_mode = snd_pcm_type(pcm);
182 if (mmap_mode == SND_PCM_TYPE_HW)
183 TRACE("mmap'd buffer is a direct hardware buffer.\n");
184 else if (mmap_mode == SND_PCM_TYPE_DMIX)
185 TRACE("mmap'd buffer is an ALSA dmix buffer\n");
186 else
187 TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode);
189 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
191 err = snd_pcm_hw_params_get_format(hw_params, &format);
192 err = snd_pcm_hw_params_get_buffer_size(hw_params, &frames);
193 err = snd_pcm_hw_params_get_channels(hw_params, &channels);
194 bits_per_sample = snd_pcm_format_physical_width(format);
195 bits_per_frame = bits_per_sample * channels;
197 if (TRACE_ON(dsalsa))
198 ALSA_TraceParameters(hw_params, NULL, FALSE);
200 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
201 snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
203 pdbi->mmap_buflen_frames = frames;
204 pdbi->mmap_buflen_bytes = snd_pcm_frames_to_bytes( pcm, frames );
206 snd_pcm_sw_params_current(pcm, sw_params);
207 snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 0);
208 snd_pcm_sw_params_get_boundary(sw_params, &boundary);
209 snd_pcm_sw_params_set_stop_threshold(pcm, sw_params, boundary);
210 snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, INT_MAX);
211 snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0);
212 snd_pcm_sw_params_set_avail_min(pcm, sw_params, 0);
213 snd_pcm_sw_params_set_xrun_mode(pcm, sw_params, SND_PCM_XRUN_NONE);
214 err = snd_pcm_sw_params(pcm, sw_params);
216 avail = snd_pcm_avail_update(pcm);
217 if (avail < 0)
219 ERR("No buffer is available: %s.\n", snd_strerror(avail));
220 return DSERR_GENERIC;
222 err = snd_pcm_mmap_begin(pcm, &areas, &ofs, &avail);
223 if ( err < 0 )
225 ERR("Can't map sound device for direct access: %s\n", snd_strerror(err));
226 return DSERR_GENERIC;
228 snd_pcm_format_set_silence(format, areas->addr, pdbi->mmap_buflen_frames);
229 pdbi->mmap_pos = ofs + snd_pcm_mmap_commit(pcm, ofs, 0);
230 pdbi->mmap_buffer = areas->addr;
232 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
233 frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
235 return DS_OK;
238 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
240 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
241 FIXME("(): stub!\n");
242 return DSERR_UNSUPPORTED;
245 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
247 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
248 ULONG refCount = InterlockedIncrement(&This->ref);
250 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
252 return refCount;
255 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
257 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
258 ULONG refCount = InterlockedDecrement(&This->ref);
260 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
262 if (refCount)
263 return refCount;
265 TRACE("mmap buffer %p destroyed\n", This->mmap_buffer);
267 if (This == This->drv->primary)
268 This->drv->primary = NULL;
270 This->pcm_crst.DebugInfo->Spare[0] = 0;
271 DeleteCriticalSection(&This->pcm_crst);
273 snd_pcm_drop(This->pcm);
274 snd_pcm_close(This->pcm);
275 This->pcm = NULL;
276 HeapFree(GetProcessHeap(), 0, This->sw_params);
277 HeapFree(GetProcessHeap(), 0, This->hw_params);
278 HeapFree(GetProcessHeap(), 0, This);
279 return 0;
282 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
283 LPVOID*ppvAudio1,LPDWORD pdwLen1,
284 LPVOID*ppvAudio2,LPDWORD pdwLen2,
285 DWORD dwWritePosition,DWORD dwWriteLen,
286 DWORD dwFlags)
288 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
289 snd_pcm_uframes_t writepos;
291 TRACE("%d bytes from %d\n", dwWriteLen, dwWritePosition);
293 /* **** */
294 EnterCriticalSection(&This->pcm_crst);
296 if (dwFlags & DSBLOCK_ENTIREBUFFER)
297 dwWriteLen = This->mmap_buflen_bytes;
299 if (dwWriteLen > This->mmap_buflen_bytes || dwWritePosition >= This->mmap_buflen_bytes)
301 /* **** */
302 LeaveCriticalSection(&This->pcm_crst);
303 return DSERR_INVALIDPARAM;
306 if (ppvAudio2) *ppvAudio2 = NULL;
307 if (pdwLen2) *pdwLen2 = 0;
309 *ppvAudio1 = (LPBYTE)This->mmap_buffer + dwWritePosition;
310 *pdwLen1 = dwWriteLen;
312 if (dwWritePosition+dwWriteLen > This->mmap_buflen_bytes)
314 DWORD remainder = This->mmap_buflen_bytes - dwWritePosition;
315 *pdwLen1 = remainder;
317 if (ppvAudio2 && pdwLen2)
319 *ppvAudio2 = This->mmap_buffer;
320 *pdwLen2 = dwWriteLen - remainder;
322 else dwWriteLen = remainder;
325 writepos = snd_pcm_bytes_to_frames(This->pcm, dwWritePosition);
326 if (writepos == This->mmap_pos)
328 const snd_pcm_channel_area_t *areas;
329 snd_pcm_uframes_t writelen = snd_pcm_bytes_to_frames(This->pcm, dwWriteLen), putin = writelen;
330 TRACE("Hit mmap_pos, locking data!\n");
331 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
334 LeaveCriticalSection(&This->pcm_crst);
335 /* **** */
336 return DS_OK;
339 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
340 LPVOID pvAudio1,DWORD dwLen1,
341 LPVOID pvAudio2,DWORD dwLen2)
343 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
344 snd_pcm_uframes_t writepos;
346 if (!dwLen1)
347 return DS_OK;
349 /* **** */
350 EnterCriticalSection(&This->pcm_crst);
352 writepos = snd_pcm_bytes_to_frames(This->pcm, (DWORD_PTR)pvAudio1 - (DWORD_PTR)This->mmap_buffer);
353 if (writepos == This->mmap_pos)
355 const snd_pcm_channel_area_t *areas;
356 snd_pcm_uframes_t writelen = snd_pcm_bytes_to_frames(This->pcm, dwLen1);
357 TRACE("Committing data\n");
358 This->mmap_pos += snd_pcm_mmap_commit(This->pcm, This->mmap_pos, writelen);
359 if (This->mmap_pos == This->mmap_buflen_frames)
360 This->mmap_pos = 0;
361 if (!This->mmap_pos && dwLen2)
363 writelen = snd_pcm_bytes_to_frames(This->pcm, dwLen2);
364 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &writelen);
365 This->mmap_pos += snd_pcm_mmap_commit(This->pcm, This->mmap_pos, writelen);
366 assert(This->mmap_pos < This->mmap_buflen_frames);
369 LeaveCriticalSection(&This->pcm_crst);
370 /* **** */
372 return DS_OK;
375 static HRESULT SetFormat(IDsDriverBufferImpl *This, LPWAVEFORMATEX pwfx)
377 snd_pcm_t *pcm = NULL;
378 snd_pcm_hw_params_t *hw_params = This->hw_params;
379 unsigned int buffer_time = 500000;
380 snd_pcm_format_t format = -1;
381 snd_pcm_uframes_t psize;
382 DWORD rate = pwfx->nSamplesPerSec;
383 int err=0;
385 switch (pwfx->wBitsPerSample)
387 case 8: format = SND_PCM_FORMAT_U8; break;
388 case 16: format = SND_PCM_FORMAT_S16_LE; break;
389 case 24: format = SND_PCM_FORMAT_S24_3LE; break;
390 case 32: format = SND_PCM_FORMAT_S32_LE; break;
391 default: FIXME("Unsupported bpp: %d\n", pwfx->wBitsPerSample); return DSERR_GENERIC;
394 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
395 if (err < 0)
397 if (errno != EBUSY || !This->pcm)
399 WARN("Cannot open sound device: %s\n", snd_strerror(err));
400 return DSERR_GENERIC;
402 snd_pcm_drop(This->pcm);
403 snd_pcm_close(This->pcm);
404 This->pcm = NULL;
405 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
406 if (err < 0)
408 WARN("Cannot open sound device: %s\n", snd_strerror(err));
409 return DSERR_BUFFERLOST;
413 /* Set some defaults */
414 snd_pcm_hw_params_any(pcm, hw_params);
415 err = snd_pcm_hw_params_set_channels(pcm, hw_params, pwfx->nChannels);
416 if (err < 0) { WARN("Could not set channels to %d\n", pwfx->nChannels); goto err; }
418 err = snd_pcm_hw_params_set_format(pcm, hw_params, format);
419 if (err < 0) { WARN("Could not set format to %d bpp\n", pwfx->wBitsPerSample); goto err; }
421 /* Alsa's rate resampling is only used if the application specifically requests
422 * a buffer at a certain frequency, else it is better to disable it due to unwanted
423 * side effects, which may include: Less granular pointer, changing buffer sizes, etc
425 #if SND_LIB_VERSION >= 0x010009
426 snd_pcm_hw_params_set_rate_resample(pcm, hw_params, 0);
427 #endif
429 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, NULL);
430 if (err < 0) { rate = pwfx->nSamplesPerSec; WARN("Could not set rate\n"); goto err; }
432 if (!ALSA_NearMatch(rate, pwfx->nSamplesPerSec))
434 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx->nSamplesPerSec, rate);
435 pwfx->nSamplesPerSec = rate;
436 pwfx->nAvgBytesPerSec = rate * pwfx->nBlockAlign;
437 /* Let DirectSound detect this */
440 snd_pcm_hw_params_set_periods_integer(pcm, hw_params);
441 snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, NULL);
442 buffer_time = 10000;
443 snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &buffer_time, NULL);
444 err = snd_pcm_hw_params(pcm, hw_params);
445 err = snd_pcm_sw_params(pcm, This->sw_params);
446 snd_pcm_prepare(pcm);
448 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
449 TRACE("Period size is: %lu\n", psize);
451 /* ALSA needs at least 3 buffers to work successfully */
452 This->mmap_commitahead = 3 * psize;
453 while (This->mmap_commitahead <= 512)
454 This->mmap_commitahead += psize;
456 if (This->pcm)
458 snd_pcm_drop(This->pcm);
459 snd_pcm_close(This->pcm);
461 This->pcm = pcm;
462 snd_pcm_prepare(This->pcm);
463 DSDB_CreateMMAP(This);
464 return S_OK;
466 err:
467 if (err < 0)
468 WARN("Failed to apply changes: %s\n", snd_strerror(err));
470 if (!This->pcm)
471 This->pcm = pcm;
472 else
473 snd_pcm_close(pcm);
475 if (This->pcm)
476 snd_pcm_hw_params_current(This->pcm, This->hw_params);
478 return DSERR_BADFORMAT;
481 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
483 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
484 HRESULT hr = S_OK;
486 TRACE("(%p, %p)\n", iface, pwfx);
488 /* **** */
489 EnterCriticalSection(&This->pcm_crst);
490 hr = SetFormat(This, pwfx);
491 /* **** */
492 LeaveCriticalSection(&This->pcm_crst);
494 if (hr == DS_OK)
495 return S_FALSE;
496 return hr;
499 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
501 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
502 FIXME("(%p,%d): stub\n",iface,dwFreq);
503 return S_OK;
506 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
508 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
509 FIXME("(%p,%p): stub\n",This,pVolPan);
510 /* TODO: Bring volume control back */
511 return DS_OK;
514 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
516 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
517 /* I don't even think alsa allows this */
518 FIXME("(%p,%d): stub\n",iface,dwNewPos);
519 return DSERR_UNSUPPORTED;
522 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
523 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
525 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
526 snd_pcm_uframes_t hw_pptr, hw_wptr;
527 snd_pcm_state_t state;
529 /* **** */
530 EnterCriticalSection(&This->pcm_crst);
532 if (!This->pcm)
534 FIXME("Bad pointer for pcm: %p\n", This->pcm);
535 LeaveCriticalSection(&This->pcm_crst);
536 return DSERR_GENERIC;
539 if (!lpdwPlay && !lpdwWrite)
540 CommitAll(This);
542 state = snd_pcm_state(This->pcm);
544 if (state != SND_PCM_STATE_PREPARED && state != SND_PCM_STATE_RUNNING)
546 CheckXRUN(This);
547 state = snd_pcm_state(This->pcm);
549 if (state == SND_PCM_STATE_RUNNING)
551 snd_pcm_uframes_t used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
553 if (This->mmap_pos > used)
554 hw_pptr = This->mmap_pos - used;
555 else
556 hw_pptr = This->mmap_buflen_frames + This->mmap_pos - used;
557 hw_pptr %= This->mmap_buflen_frames;
559 TRACE("At position: %ld (%ld) - Used %ld\n", hw_pptr, This->mmap_pos, used);
561 else hw_pptr = This->mmap_pos;
562 hw_wptr = This->mmap_pos;
564 LeaveCriticalSection(&This->pcm_crst);
565 /* **** */
567 if (lpdwPlay)
568 *lpdwPlay = snd_pcm_frames_to_bytes(This->pcm, hw_pptr);
569 if (lpdwWrite)
570 *lpdwWrite = snd_pcm_frames_to_bytes(This->pcm, hw_wptr);
572 TRACE("hw_pptr=0x%08x, hw_wptr=0x%08x playpos=%d, writepos=%d\n", (unsigned int)hw_pptr, (unsigned int)hw_wptr, lpdwPlay?*lpdwPlay:-1, lpdwWrite?*lpdwWrite:-1);
573 return DS_OK;
576 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
578 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
579 TRACE("(%p,%x,%x,%x)\n",iface,dwRes1,dwRes2,dwFlags);
581 /* **** */
582 EnterCriticalSection(&This->pcm_crst);
583 snd_pcm_start(This->pcm);
584 /* **** */
585 LeaveCriticalSection(&This->pcm_crst);
586 return DS_OK;
589 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
591 const snd_pcm_channel_area_t *areas;
592 snd_pcm_uframes_t avail;
593 snd_pcm_format_t format;
594 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
595 TRACE("(%p)\n",iface);
597 /* **** */
598 EnterCriticalSection(&This->pcm_crst);
599 avail = This->mmap_buflen_frames;
600 snd_pcm_drop(This->pcm);
601 snd_pcm_prepare(This->pcm);
602 avail = snd_pcm_avail_update(This->pcm);
603 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &avail);
604 snd_pcm_hw_params_get_format(This->hw_params, &format);
605 snd_pcm_format_set_silence(format, areas->addr, This->mmap_buflen_frames);
606 snd_pcm_mmap_commit(This->pcm, This->mmap_pos, 0);
608 /* **** */
609 LeaveCriticalSection(&This->pcm_crst);
610 return DS_OK;
613 static const IDsDriverBufferVtbl dsdbvt =
615 IDsDriverBufferImpl_QueryInterface,
616 IDsDriverBufferImpl_AddRef,
617 IDsDriverBufferImpl_Release,
618 IDsDriverBufferImpl_Lock,
619 IDsDriverBufferImpl_Unlock,
620 IDsDriverBufferImpl_SetFormat,
621 IDsDriverBufferImpl_SetFrequency,
622 IDsDriverBufferImpl_SetVolumePan,
623 IDsDriverBufferImpl_SetPosition,
624 IDsDriverBufferImpl_GetPosition,
625 IDsDriverBufferImpl_Play,
626 IDsDriverBufferImpl_Stop
629 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
631 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
632 FIXME("(%p): stub!\n",iface);
633 return DSERR_UNSUPPORTED;
636 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
638 IDsDriverImpl *This = (IDsDriverImpl *)iface;
639 ULONG refCount = InterlockedIncrement(&This->ref);
641 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
643 return refCount;
646 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
648 IDsDriverImpl *This = (IDsDriverImpl *)iface;
649 ULONG refCount = InterlockedDecrement(&This->ref);
651 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
653 if (refCount)
654 return refCount;
656 HeapFree(GetProcessHeap(), 0, This);
657 return 0;
660 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
662 IDsDriverImpl *This = (IDsDriverImpl *)iface;
663 TRACE("(%p,%p)\n",iface,pDesc);
664 memcpy(pDesc, &(WOutDev[This->wDevID].ds_desc), sizeof(DSDRIVERDESC));
665 pDesc->dwFlags = DSDDESC_DONTNEEDSECONDARYLOCK | DSDDESC_DONTNEEDWRITELEAD;
666 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
667 pDesc->wVxdId = 0;
668 pDesc->wReserved = 0;
669 pDesc->ulDeviceNum = This->wDevID;
670 pDesc->dwHeapType = DSDHEAP_NOHEAP;
671 pDesc->pvDirectDrawHeap = NULL;
672 pDesc->dwMemStartAddress = 0xDEAD0000;
673 pDesc->dwMemEndAddress = 0xDEAF0000;
674 pDesc->dwMemAllocExtra = 0;
675 pDesc->pvReserved1 = NULL;
676 pDesc->pvReserved2 = NULL;
677 return DS_OK;
680 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
682 HRESULT hr = S_OK;
683 IDsDriverImpl *This = (IDsDriverImpl *)iface;
684 int err=0;
685 snd_pcm_t *pcm = NULL;
686 snd_pcm_hw_params_t *hw_params;
688 /* While this is not really needed, it is a good idea to do this,
689 * to see if sound can be initialized */
691 hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
693 if (!hw_params)
695 hr = DSERR_OUTOFMEMORY;
696 goto unalloc;
699 err = snd_pcm_open(&pcm, WOutDev[This->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
700 if (err < 0) goto err;
701 err = snd_pcm_hw_params_any(pcm, hw_params);
702 if (err < 0) goto err;
703 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
704 if (err < 0) goto err;
706 TRACE("Success\n");
707 snd_pcm_close(pcm);
708 goto unalloc;
710 err:
711 hr = DSERR_GENERIC;
712 FIXME("Failed to open device: %s\n", snd_strerror(err));
713 if (pcm)
714 snd_pcm_close(pcm);
715 unalloc:
716 HeapFree(GetProcessHeap(), 0, hw_params);
717 if (hr != S_OK)
718 WARN("--> %08x\n", hr);
719 return hr;
722 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
724 IDsDriverImpl *This = (IDsDriverImpl *)iface;
725 TRACE("(%p) stub, harmless\n",This);
726 return DS_OK;
729 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
731 IDsDriverImpl *This = (IDsDriverImpl *)iface;
732 TRACE("(%p,%p)\n",iface,pCaps);
733 memcpy(pCaps, &(WOutDev[This->wDevID].ds_caps), sizeof(DSDRIVERCAPS));
734 return DS_OK;
737 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
738 LPWAVEFORMATEX pwfx,
739 DWORD dwFlags, DWORD dwCardAddress,
740 LPDWORD pdwcbBufferSize,
741 LPBYTE *ppbBuffer,
742 LPVOID *ppvObj)
744 IDsDriverImpl *This = (IDsDriverImpl *)iface;
745 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
746 HRESULT err;
748 TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
749 /* we only support primary buffers... for now */
750 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
751 return DSERR_UNSUPPORTED;
752 if (This->primary)
753 return DSERR_ALLOCATED;
755 *ippdsdb = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsDriverBufferImpl));
756 if (*ippdsdb == NULL)
757 return DSERR_OUTOFMEMORY;
759 (*ippdsdb)->hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
760 (*ippdsdb)->sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
761 if (!(*ippdsdb)->hw_params || !(*ippdsdb)->sw_params)
763 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
764 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
765 return DSERR_OUTOFMEMORY;
767 (*ippdsdb)->lpVtbl = &dsdbvt;
768 (*ippdsdb)->ref = 1;
769 (*ippdsdb)->drv = This;
770 InitializeCriticalSection(&(*ippdsdb)->pcm_crst);
771 (*ippdsdb)->pcm_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_DSOUTPUT.pcm_crst");
773 /* SetFormat has to re-initialize pcm here anyway */
774 err = SetFormat(*ippdsdb, pwfx);
775 if (FAILED(err))
777 WARN("Error occurred: %08x\n", err);
778 goto err;
781 if (dwFlags & DSBCAPS_PRIMARYBUFFER)
782 This->primary = *ippdsdb;
784 *pdwcbBufferSize = (*ippdsdb)->mmap_buflen_bytes;
785 *ppbBuffer = (*ippdsdb)->mmap_buffer;
787 /* buffer is ready to go */
788 TRACE("buffer created at %p\n", *ippdsdb);
789 return err;
791 err:
792 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
793 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
794 HeapFree(GetProcessHeap(), 0, *ippdsdb);
795 *ippdsdb = NULL;
796 return err;
799 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
800 PIDSDRIVERBUFFER pBuffer,
801 LPVOID *ppvObj)
803 IDsDriverImpl *This = (IDsDriverImpl *)iface;
804 FIXME("(%p,%p): stub\n",This,pBuffer);
805 return DSERR_INVALIDCALL;
808 static const IDsDriverVtbl dsdvt =
810 IDsDriverImpl_QueryInterface,
811 IDsDriverImpl_AddRef,
812 IDsDriverImpl_Release,
813 IDsDriverImpl_GetDriverDesc,
814 IDsDriverImpl_Open,
815 IDsDriverImpl_Close,
816 IDsDriverImpl_GetCaps,
817 IDsDriverImpl_CreateSoundBuffer,
818 IDsDriverImpl_DuplicateSoundBuffer
821 DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
823 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
825 TRACE("driver created\n");
827 /* the HAL isn't much better than the HEL if we can't do mmap() */
828 if (!(WOutDev[wDevID].outcaps.dwSupport & WAVECAPS_DIRECTSOUND))
830 WARN("MMAP not supported for this device, falling back to waveout, should be harmless\n");
831 return MMSYSERR_NOTSUPPORTED;
834 *idrv = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
835 if (!*idrv)
836 return MMSYSERR_NOMEM;
837 (*idrv)->lpVtbl = &dsdvt;
838 (*idrv)->ref = 1;
840 (*idrv)->wDevID = wDevID;
841 (*idrv)->primary = NULL;
842 return MMSYSERR_NOERROR;
845 DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
847 memcpy(desc, &(WOutDev[wDevID].ds_desc), sizeof(DSDRIVERDESC));
848 return MMSYSERR_NOERROR;
851 #endif /* HAVE_ALSA */