1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
6 * Copyright 1993 Martin Ayotte
7 * 1998-2002 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(winmm
);
40 typedef struct tagWINE_PLAYSOUND
50 static WINE_PLAYSOUND
*PlaySoundCurrent
;
51 static BOOL bPlaySoundStop
;
53 static HMMIO
get_mmioFromFile(LPCWSTR lpszName
)
58 static const WCHAR dotwav
[] = {'.','w','a','v',0};
60 ret
= mmioOpenW((LPWSTR
)lpszName
, NULL
,
61 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
62 if (ret
!= 0) return ret
;
63 if (SearchPathW(NULL
, lpszName
, dotwav
, sizeof(buf
)/sizeof(buf
[0]), buf
, &dummy
))
65 return mmioOpenW(buf
, NULL
,
66 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
71 static HMMIO
get_mmioFromProfile(UINT uFlags
, LPCWSTR lpszName
)
76 HKEY hRegSnd
, hRegApp
, hScheme
, hSnd
;
77 DWORD err
, type
, count
;
79 static const WCHAR wszSounds
[] = {'S','o','u','n','d','s',0};
80 static const WCHAR wszDefault
[] = {'D','e','f','a','u','l','t',0};
81 static const WCHAR wszKey
[] = {'A','p','p','E','v','e','n','t','s','\\',
82 'S','c','h','e','m','e','s','\\',
84 static const WCHAR wszDotDefault
[] = {'.','D','e','f','a','u','l','t',0};
85 static const WCHAR wszDotCurrent
[] = {'.','C','u','r','r','e','n','t',0};
86 static const WCHAR wszNull
[] = {0};
88 TRACE("searching in SystemSound list for %s\n", debugstr_w(lpszName
));
89 GetProfileStringW(wszSounds
, lpszName
, wszNull
, str
, sizeof(str
)/sizeof(str
[0]));
90 if (lstrlenW(str
) == 0)
92 if (uFlags
& SND_NODEFAULT
) goto next
;
93 GetProfileStringW(wszSounds
, wszDefault
, wszNull
, str
, sizeof(str
)/sizeof(str
[0]));
94 if (lstrlenW(str
) == 0) goto next
;
96 for (ptr
= str
; *ptr
&& *ptr
!= ','; ptr
++);
98 hmmio
= mmioOpenW(str
, NULL
, MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
99 if (hmmio
!= 0) return hmmio
;
101 /* we look up the registry under
102 * HKCU\AppEvents\Schemes\Apps\.Default
103 * HKCU\AppEvents\Schemes\Apps\<AppName>
105 if (RegOpenKeyW(HKEY_CURRENT_USER
, wszKey
, &hRegSnd
) != 0) goto none
;
106 if (uFlags
& SND_APPLICATION
)
111 len
= GetModuleFileNameW(0, str
, sizeof(str
)/sizeof(str
[0]));
112 if (len
> 0 && len
< sizeof(str
)/sizeof(str
[0]))
114 for (ptr
= str
+ lstrlenW(str
) - 1; ptr
>= str
; ptr
--)
116 if (*ptr
== '.') *ptr
= 0;
119 err
= RegOpenKeyW(hRegSnd
, ptr
+1, &hRegApp
);
127 err
= RegOpenKeyW(hRegSnd
, wszDotDefault
, &hRegApp
);
129 RegCloseKey(hRegSnd
);
130 if (err
!= 0) goto none
;
131 err
= RegOpenKeyW(hRegApp
, lpszName
, &hScheme
);
132 RegCloseKey(hRegApp
);
133 if (err
!= 0) goto none
;
134 /* what's the difference between .Current and .Default ? */
135 err
= RegOpenKeyW(hScheme
, wszDotDefault
, &hSnd
);
138 err
= RegOpenKeyW(hScheme
, wszDotCurrent
, &hSnd
);
139 RegCloseKey(hScheme
);
144 err
= RegQueryValueExW(hSnd
, NULL
, 0, &type
, (LPBYTE
)str
, &count
);
146 if (err
!= 0 || !*str
) goto none
;
147 hmmio
= mmioOpenW(str
, NULL
, MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
148 if (hmmio
) return hmmio
;
150 WARN("can't find SystemSound=%s !\n", debugstr_w(lpszName
));
154 struct playsound_data
160 static void CALLBACK
PlaySound_Callback(HWAVEOUT hwo
, UINT uMsg
,
161 DWORD_PTR dwInstance
,
162 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
164 struct playsound_data
* s
= (struct playsound_data
*)dwInstance
;
171 InterlockedIncrement(&s
->dwEventCount
);
172 TRACE("Returning waveHdr=%lx\n", dwParam1
);
176 ERR("Unknown uMsg=%d\n", uMsg
);
180 static void PlaySound_WaitDone(struct playsound_data
* s
)
183 if (InterlockedDecrement(&s
->dwEventCount
) >= 0) break;
184 InterlockedIncrement(&s
->dwEventCount
);
186 WaitForSingleObject(s
->hEvent
, INFINITE
);
190 static BOOL
PlaySound_IsString(DWORD fdwSound
, const void* psz
)
192 /* SND_RESOURCE is 0x40004 while
193 * SND_MEMORY is 0x00004
195 switch (fdwSound
& (SND_RESOURCE
|SND_ALIAS_ID
|SND_FILENAME
))
197 case SND_RESOURCE
: return HIWORD(psz
) != 0; /* by name or by ID ? */
199 case SND_MEMORY
: return FALSE
;
202 case SND_ALIAS
|SND_FILENAME
:
204 default: FIXME("WTF\n"); return FALSE
;
208 static void PlaySound_Free(WINE_PLAYSOUND
* wps
)
210 EnterCriticalSection(&WINMM_cs
);
211 PlaySoundCurrent
= NULL
;
212 SetEvent(psLastEvent
);
213 LeaveCriticalSection(&WINMM_cs
);
214 if (wps
->bAlloc
) HeapFree(GetProcessHeap(), 0, (void*)wps
->pszSound
);
215 HeapFree(GetProcessHeap(), 0, wps
);
218 static WINE_PLAYSOUND
* PlaySound_Alloc(const void* pszSound
, HMODULE hmod
,
219 DWORD fdwSound
, BOOL bUnicode
)
223 wps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*wps
));
224 if (!wps
) return NULL
;
227 wps
->fdwSound
= fdwSound
;
228 if (PlaySound_IsString(fdwSound
, pszSound
))
232 if (fdwSound
& SND_ASYNC
)
234 LPWSTR sound
= HeapAlloc(GetProcessHeap(), 0,
235 (lstrlenW(pszSound
)+1) * sizeof(WCHAR
));
236 if (!sound
) goto oom_error
;
237 wps
->pszSound
= lstrcpyW(sound
, pszSound
);
241 wps
->pszSound
= pszSound
;
245 UNICODE_STRING usBuffer
;
246 RtlCreateUnicodeStringFromAsciiz(&usBuffer
, pszSound
);
247 wps
->pszSound
= usBuffer
.Buffer
;
248 if (!wps
->pszSound
) goto oom_error
;
253 wps
->pszSound
= pszSound
;
257 if (wps
->bAlloc
) HeapFree(GetProcessHeap(), 0, (void*)wps
->pszSound
);
258 HeapFree(GetProcessHeap(), 0, wps
);
262 static DWORD WINAPI
proc_PlaySound(LPVOID arg
)
264 WINE_PLAYSOUND
* wps
= arg
;
269 LPWAVEFORMATEX lpWaveFormat
= NULL
;
271 LPWAVEHDR waveHdr
= NULL
;
272 INT count
, bufsize
, left
, index
;
273 struct playsound_data s
;
278 TRACE("SoundName=%s !\n", debugstr_w(wps
->pszSound
));
280 /* if resource, grab it */
281 if ((wps
->fdwSound
& SND_RESOURCE
) == SND_RESOURCE
) {
282 static const WCHAR wszWave
[] = {'W','A','V','E',0};
286 if ((hRes
= FindResourceW(wps
->hMod
, wps
->pszSound
, wszWave
)) == 0 ||
287 (hGlob
= LoadResource(wps
->hMod
, hRes
)) == 0)
289 if ((data
= LockResource(hGlob
)) == NULL
) {
295 data
= (void*)wps
->pszSound
;
297 /* construct an MMIO stream (either in memory, or from a file */
298 if (wps
->fdwSound
& SND_MEMORY
)
299 { /* NOTE: SND_RESOURCE has the SND_MEMORY bit set */
302 memset(&mminfo
, 0, sizeof(mminfo
));
303 mminfo
.fccIOProc
= FOURCC_MEM
;
304 mminfo
.pchBuffer
= data
;
305 mminfo
.cchBuffer
= -1; /* FIXME: when a resource, could grab real size */
306 TRACE("Memory sound %p\n", data
);
307 hmmio
= mmioOpenW(NULL
, &mminfo
, MMIO_READ
);
309 if (!hmmio
&& wps
->fdwSound
& SND_ALIAS
)
311 if ((wps
->fdwSound
& SND_ALIAS_ID
) == SND_ALIAS_ID
)
313 static const WCHAR wszSystemAsterisk
[] = {'S','y','s','t','e','m','A','s','t','e','r','i','s','k',0};
314 static const WCHAR wszSystemDefault
[] = {'S','y','s','t','e','m','D','e','f','a','u','l','t',0};
315 static const WCHAR wszSystemExclamation
[] = {'S','y','s','t','e','m','E','x','c','l','a','m','a','t','i','o','n',0};
316 static const WCHAR wszSystemExit
[] = {'S','y','s','t','e','m','E','x','i','t',0};
317 static const WCHAR wszSystemHand
[] = {'S','y','s','t','e','m','H','a','n','d',0};
318 static const WCHAR wszSystemQuestion
[] = {'S','y','s','t','e','m','Q','u','e','s','t','i','o','n',0};
319 static const WCHAR wszSystemStart
[] = {'S','y','s','t','e','m','S','t','a','r','t',0};
320 static const WCHAR wszSystemWelcome
[] = {'S','y','s','t','e','m','W','e','l','c','o','m','e',0};
322 wps
->fdwSound
&= ~(SND_ALIAS_ID
^ SND_ALIAS
);
323 if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMASTERISK
)
324 wps
->pszSound
= wszSystemAsterisk
;
325 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMDEFAULT
)
326 wps
->pszSound
= wszSystemDefault
;
327 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMEXCLAMATION
)
328 wps
->pszSound
= wszSystemExclamation
;
329 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMEXIT
)
330 wps
->pszSound
= wszSystemExit
;
331 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMHAND
)
332 wps
->pszSound
= wszSystemHand
;
333 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMQUESTION
)
334 wps
->pszSound
= wszSystemQuestion
;
335 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMSTART
)
336 wps
->pszSound
= wszSystemStart
;
337 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMWELCOME
)
338 wps
->pszSound
= wszSystemWelcome
;
339 else goto errCleanUp
;
341 hmmio
= get_mmioFromProfile(wps
->fdwSound
, wps
->pszSound
);
344 wps
->fdwSound
&= ~SND_ALIAS
;
345 wps
->fdwSound
|= SND_FILENAME
;
348 if (!hmmio
&& wps
->fdwSound
& SND_FILENAME
)
350 hmmio
= get_mmioFromFile(wps
->pszSound
);
352 if (!(wps
->fdwSound
& (SND_FILENAME
|SND_ALIAS
|SND_MEMORY
)))
354 if ((hmmio
= get_mmioFromProfile(wps
->fdwSound
| SND_NODEFAULT
, wps
->pszSound
)) == 0)
356 if ((hmmio
= get_mmioFromFile(wps
->pszSound
)) == 0)
358 hmmio
= get_mmioFromProfile(wps
->fdwSound
, wps
->pszSound
);
362 if (hmmio
== 0) goto errCleanUp
;
364 if (mmioDescend(hmmio
, &ckMainRIFF
, NULL
, 0))
367 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
368 (LPSTR
)&ckMainRIFF
.ckid
, (LPSTR
)&ckMainRIFF
.fccType
, ckMainRIFF
.cksize
);
370 if ((ckMainRIFF
.ckid
!= FOURCC_RIFF
) ||
371 (ckMainRIFF
.fccType
!= mmioFOURCC('W', 'A', 'V', 'E')))
374 mmckInfo
.ckid
= mmioFOURCC('f', 'm', 't', ' ');
375 if (mmioDescend(hmmio
, &mmckInfo
, &ckMainRIFF
, MMIO_FINDCHUNK
))
378 TRACE("Chunk Found ckid=%.4s fccType=%08x cksize=%08X\n",
379 (LPSTR
)&mmckInfo
.ckid
, mmckInfo
.fccType
, mmckInfo
.cksize
);
381 lpWaveFormat
= HeapAlloc(GetProcessHeap(), 0, mmckInfo
.cksize
);
384 if (mmioRead(hmmio
, (HPSTR
)lpWaveFormat
, mmckInfo
.cksize
) < sizeof(PCMWAVEFORMAT
))
387 TRACE("wFormatTag=%04X !\n", lpWaveFormat
->wFormatTag
);
388 TRACE("nChannels=%d\n", lpWaveFormat
->nChannels
);
389 TRACE("nSamplesPerSec=%d\n", lpWaveFormat
->nSamplesPerSec
);
390 TRACE("nAvgBytesPerSec=%d\n", lpWaveFormat
->nAvgBytesPerSec
);
391 TRACE("nBlockAlign=%d\n", lpWaveFormat
->nBlockAlign
);
392 TRACE("wBitsPerSample=%u !\n", lpWaveFormat
->wBitsPerSample
);
394 /* move to end of 'fmt ' chunk */
395 mmioAscend(hmmio
, &mmckInfo
, 0);
397 mmckInfo
.ckid
= mmioFOURCC('d', 'a', 't', 'a');
398 if (mmioDescend(hmmio
, &mmckInfo
, &ckMainRIFF
, MMIO_FINDCHUNK
))
401 TRACE("Chunk Found ckid=%.4s fccType=%08x cksize=%08X\n",
402 (LPSTR
)&mmckInfo
.ckid
, mmckInfo
.fccType
, mmckInfo
.cksize
);
404 s
.hEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
405 if (!s
.hEvent
|| bPlaySoundStop
)
408 if (waveOutOpen(&hWave
, WAVE_MAPPER
, lpWaveFormat
, (DWORD_PTR
)PlaySound_Callback
,
409 (DWORD_PTR
)&s
, CALLBACK_FUNCTION
) != MMSYSERR_NOERROR
)
412 /* make it so that 3 buffers per second are needed */
413 bufsize
= (((lpWaveFormat
->nAvgBytesPerSec
/ 3) - 1) / lpWaveFormat
->nBlockAlign
+ 1) *
414 lpWaveFormat
->nBlockAlign
;
415 waveHdr
= HeapAlloc(GetProcessHeap(), 0, 2 * sizeof(WAVEHDR
) + 2 * bufsize
);
418 waveHdr
[0].lpData
= (char*)waveHdr
+ 2 * sizeof(WAVEHDR
);
419 waveHdr
[1].lpData
= (char*)waveHdr
+ 2 * sizeof(WAVEHDR
) + bufsize
;
420 waveHdr
[0].dwUser
= waveHdr
[1].dwUser
= 0L;
421 waveHdr
[0].dwLoops
= waveHdr
[1].dwLoops
= 0L;
422 waveHdr
[0].dwFlags
= waveHdr
[1].dwFlags
= 0L;
423 waveHdr
[0].dwBufferLength
= waveHdr
[1].dwBufferLength
= bufsize
;
424 if (waveOutPrepareHeader(hWave
, &waveHdr
[0], sizeof(WAVEHDR
)) ||
425 waveOutPrepareHeader(hWave
, &waveHdr
[1], sizeof(WAVEHDR
))) {
430 s
.dwEventCount
= 1L; /* for first buffer */
434 left
= mmckInfo
.cksize
;
436 mmioSeek(hmmio
, mmckInfo
.dwDataOffset
, SEEK_SET
);
444 count
= mmioRead(hmmio
, waveHdr
[index
].lpData
, min(bufsize
, left
));
445 if (count
< 1) break;
447 waveHdr
[index
].dwBufferLength
= count
;
448 if (waveOutWrite(hWave
, &waveHdr
[index
], sizeof(WAVEHDR
)) == MMSYSERR_NOERROR
) {
450 PlaySound_WaitDone(&s
);
453 ERR("Aborting play loop, waveOutWrite error\n");
459 } while (wps
->bLoop
);
461 PlaySound_WaitDone(&s
); /* to balance first buffer */
464 waveOutUnprepareHeader(hWave
, &waveHdr
[0], sizeof(WAVEHDR
));
465 waveOutUnprepareHeader(hWave
, &waveHdr
[1], sizeof(WAVEHDR
));
468 TRACE("Done playing=%s => %s!\n", debugstr_w(wps
->pszSound
), bRet
? "ok" : "ko");
469 HeapFree(GetProcessHeap(), 0, lpWaveFormat
);
472 EnterCriticalSection(&WINMM_cs
);
473 /* the CS prevents a concurrent waveOutReset */
475 LeaveCriticalSection(&WINMM_cs
);
476 while (waveOutClose(hWave
) == WAVERR_STILLPLAYING
)
479 CloseHandle(s
.hEvent
);
480 HeapFree(GetProcessHeap(), 0, waveHdr
);
481 if (hmmio
) mmioClose(hmmio
, 0);
488 static BOOL
MULTIMEDIA_PlaySound(const void* pszSound
, HMODULE hmod
, DWORD fdwSound
, BOOL bUnicode
)
490 WINE_PLAYSOUND
* wps
= NULL
;
492 TRACE("pszSound='%p' hmod=%p fdwSound=%08X\n",
493 pszSound
, hmod
, fdwSound
);
495 /* SND_NOWAIT is ignored in w95/2k/xp. */
496 if ((fdwSound
& SND_NOSTOP
) && PlaySoundCurrent
!= NULL
)
499 /* alloc internal structure, if we need to play something */
500 if (pszSound
&& !(fdwSound
& SND_PURGE
))
502 if (!(wps
= PlaySound_Alloc(pszSound
, hmod
, fdwSound
, bUnicode
)))
506 EnterCriticalSection(&WINMM_cs
);
507 /* since several threads can enter PlaySound in parallel, we're not
508 * sure, at this point, that another thread didn't start a new playsound
510 while (PlaySoundCurrent
!= NULL
)
512 ResetEvent(psLastEvent
);
513 /* FIXME: doc says we have to stop all instances of pszSound if it's non
514 * NULL... as of today, we stop all playing instances */
515 bPlaySoundStop
= TRUE
;
516 if(PlaySoundCurrent
->hWave
)
517 waveOutReset(PlaySoundCurrent
->hWave
);
519 LeaveCriticalSection(&WINMM_cs
);
520 WaitForSingleObject(psLastEvent
, INFINITE
);
521 EnterCriticalSection(&WINMM_cs
);
523 bPlaySoundStop
= FALSE
;
526 PlaySoundCurrent
= wps
;
527 LeaveCriticalSection(&WINMM_cs
);
529 if (!wps
) return TRUE
;
531 if (fdwSound
& SND_ASYNC
)
534 wps
->bLoop
= (fdwSound
& SND_LOOP
) != 0;
535 if ((handle
= CreateThread(NULL
, 0, proc_PlaySound
, wps
, 0, NULL
)) != 0) {
536 SetThreadPriority(handle
, THREAD_PRIORITY_TIME_CRITICAL
);
541 else return proc_PlaySound(wps
);
548 /**************************************************************************
549 * PlaySoundA [WINMM.@]
551 BOOL WINAPI
PlaySoundA(LPCSTR pszSoundA
, HMODULE hmod
, DWORD fdwSound
)
553 return MULTIMEDIA_PlaySound(pszSoundA
, hmod
, fdwSound
, FALSE
);
556 /**************************************************************************
557 * PlaySoundW [WINMM.@]
559 BOOL WINAPI
PlaySoundW(LPCWSTR pszSoundW
, HMODULE hmod
, DWORD fdwSound
)
561 return MULTIMEDIA_PlaySound(pszSoundW
, hmod
, fdwSound
, TRUE
);
564 /**************************************************************************
565 * sndPlaySoundA [WINMM.@]
567 BOOL WINAPI
sndPlaySoundA(LPCSTR pszSoundA
, UINT uFlags
)
569 uFlags
&= SND_RESOURCE
|SND_ALIAS_ID
|SND_FILENAME
|SND_ASYNC
|SND_LOOP
|SND_MEMORY
|SND_NODEFAULT
|SND_NOSTOP
|SND_SYNC
;
570 return MULTIMEDIA_PlaySound(pszSoundA
, 0, uFlags
, FALSE
);
573 /**************************************************************************
574 * sndPlaySoundW [WINMM.@]
576 BOOL WINAPI
sndPlaySoundW(LPCWSTR pszSound
, UINT uFlags
)
578 uFlags
&= SND_RESOURCE
|SND_ALIAS_ID
|SND_FILENAME
|SND_ASYNC
|SND_LOOP
|SND_MEMORY
|SND_NODEFAULT
|SND_NOSTOP
|SND_SYNC
;
579 return MULTIMEDIA_PlaySound(pszSound
, 0, uFlags
, TRUE
);
582 /**************************************************************************
583 * mmsystemGetVersion [WINMM.@]
585 UINT WINAPI
mmsystemGetVersion(void)
587 TRACE("3.10 (Win95?)\n");