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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(winmm
);
38 static HMMIO
get_mmioFromFile(LPCWSTR lpszName
)
44 ret
= mmioOpenW((LPWSTR
)lpszName
, NULL
,
45 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
46 if (ret
!= 0) return ret
;
47 if (SearchPathW(NULL
, lpszName
, NULL
, sizeof(buf
)/sizeof(buf
[0]), buf
, &dummy
))
49 return mmioOpenW(buf
, NULL
,
50 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
55 static HMMIO
get_mmioFromProfile(UINT uFlags
, LPCWSTR lpszName
)
60 HKEY hRegSnd
, hRegApp
, hScheme
, hSnd
;
61 DWORD err
, type
, count
;
63 static WCHAR wszSounds
[] = {'S','o','u','n','d','s',0};
64 static WCHAR wszDefault
[] = {'D','e','f','a','u','l','t',0};
65 static WCHAR wszKey
[] = {'A','p','p','E','v','e','n','t','s','\\',
66 'S','c','h','e','m','e','s','\\',
68 static WCHAR wszDotDefault
[] = {'.','D','e','f','a','u','l','t',0};
69 static WCHAR wszNull
[] = {0};
71 TRACE("searching in SystemSound list for %s\n", debugstr_w(lpszName
));
72 GetProfileStringW(wszSounds
, (LPWSTR
)lpszName
, wszNull
, str
, sizeof(str
)/sizeof(str
[0]));
73 if (lstrlenW(str
) == 0)
75 if (uFlags
& SND_NODEFAULT
) goto next
;
76 GetProfileStringW(wszSounds
, wszDefault
, wszNull
, str
, sizeof(str
)/sizeof(str
[0]));
77 if (lstrlenW(str
) == 0) goto next
;
79 for (ptr
= str
; *ptr
&& *ptr
!= ','; ptr
++);
81 hmmio
= mmioOpenW(str
, NULL
, MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
82 if (hmmio
!= 0) return hmmio
;
84 /* we look up the registry under
85 * HKCU\AppEvents\Schemes\Apps\.Default
86 * HKCU\AppEvents\Schemes\Apps\<AppName>
88 if (RegOpenKeyW(HKEY_CURRENT_USER
, wszKey
, &hRegSnd
) != 0) goto none
;
89 if (uFlags
& SND_APPLICATION
)
92 if (GetModuleFileNameW(0, str
, sizeof(str
)/sizeof(str
[0])))
94 for (ptr
= str
+ lstrlenW(str
) - 1; ptr
>= str
; ptr
--)
96 if (*ptr
== '.') *ptr
= 0;
99 err
= RegOpenKeyW(hRegSnd
, str
, &hRegApp
);
107 err
= RegOpenKeyW(hRegSnd
, wszDotDefault
, &hRegApp
);
109 RegCloseKey(hRegSnd
);
110 if (err
!= 0) goto none
;
111 err
= RegOpenKeyW(hRegApp
, lpszName
, &hScheme
);
112 RegCloseKey(hRegApp
);
113 if (err
!= 0) goto none
;
114 err
= RegOpenKeyW(hScheme
, wszDotDefault
, &hSnd
);
115 RegCloseKey(hScheme
);
116 if (err
!= 0) goto none
;
117 count
= sizeof(str
)/sizeof(str
[0]);
118 err
= RegQueryValueExW(hSnd
, NULL
, 0, &type
, (LPBYTE
)str
, &count
);
120 if (err
!= 0 || !*str
) goto none
;
121 hmmio
= mmioOpenW(str
, NULL
, MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
122 if (hmmio
) return hmmio
;
124 WARN("can't find SystemSound='%s' !\n", debugstr_w(lpszName
));
128 struct playsound_data
134 static void CALLBACK
PlaySound_Callback(HWAVEOUT hwo
, UINT uMsg
,
136 DWORD dwParam1
, DWORD dwParam2
)
138 struct playsound_data
* s
= (struct playsound_data
*)dwInstance
;
145 InterlockedIncrement(&s
->dwEventCount
);
146 TRACE("Returning waveHdr=%lx\n", dwParam1
);
150 ERR("Unknown uMsg=%d\n", uMsg
);
154 static void PlaySound_WaitDone(struct playsound_data
* s
)
157 ResetEvent(s
->hEvent
);
158 if (InterlockedDecrement(&s
->dwEventCount
) >= 0) break;
159 InterlockedIncrement(&s
->dwEventCount
);
161 WaitForSingleObject(s
->hEvent
, INFINITE
);
165 static BOOL
PlaySound_IsString(DWORD fdwSound
, const void* psz
)
167 /* SND_RESOURCE is 0x40004 while
168 * SND_MEMORY is 0x00004
170 switch (fdwSound
& (SND_RESOURCE
|SND_ALIAS
|SND_FILENAME
))
172 case SND_RESOURCE
: return HIWORD(psz
) != 0; /* by name or by ID ? */
173 case SND_MEMORY
: return FALSE
;
174 case SND_ALIAS
: /* what about ALIAS_ID ??? */
177 default: FIXME("WTF\n"); return FALSE
;
181 static void PlaySound_Free(WINE_PLAYSOUND
* wps
)
185 EnterCriticalSection(&WINMM_IData
->cs
);
186 for (p
= &WINMM_IData
->lpPlaySound
; *p
&& *p
!= wps
; p
= &((*p
)->lpNext
));
187 if (*p
) *p
= (*p
)->lpNext
;
188 if (WINMM_IData
->lpPlaySound
== NULL
) SetEvent(WINMM_IData
->psLastEvent
);
189 LeaveCriticalSection(&WINMM_IData
->cs
);
190 if (wps
->bAlloc
) HeapFree(GetProcessHeap(), 0, (void*)wps
->pszSound
);
191 HeapFree(GetProcessHeap(), 0, wps
);
194 static WINE_PLAYSOUND
* PlaySound_Alloc(const void* pszSound
, HMODULE hmod
,
195 DWORD fdwSound
, BOOL bUnicode
)
199 wps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*wps
));
200 if (!wps
) return NULL
;
203 wps
->fdwSound
= fdwSound
;
204 if (PlaySound_IsString(fdwSound
, pszSound
))
208 if (fdwSound
& SND_ASYNC
)
210 wps
->pszSound
= HeapAlloc(GetProcessHeap(), 0,
211 (lstrlenW(pszSound
)+1) * sizeof(WCHAR
));
212 if (!wps
->pszSound
) goto oom_error
;
213 lstrcpyW((LPWSTR
)wps
->pszSound
, pszSound
);
217 wps
->pszSound
= pszSound
;
221 wps
->pszSound
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszSound
);
222 if (!wps
->pszSound
) goto oom_error
;
227 wps
->pszSound
= pszSound
;
235 static DWORD WINAPI
proc_PlaySound(LPVOID arg
)
237 WINE_PLAYSOUND
* wps
= (WINE_PLAYSOUND
*)arg
;
242 LPWAVEFORMATEX lpWaveFormat
= NULL
;
244 LPWAVEHDR waveHdr
= NULL
;
245 INT count
, bufsize
, left
, index
;
246 struct playsound_data s
;
251 TRACE("SoundName='%s' !\n", debugstr_w(wps
->pszSound
));
253 /* if resource, grab it */
254 if ((wps
->fdwSound
& SND_RESOURCE
) == SND_RESOURCE
) {
255 static WCHAR wszWave
[] = {'W','A','V','E',0};
259 if ((hRes
= FindResourceW(wps
->hMod
, wps
->pszSound
, wszWave
)) == 0 ||
260 (hGlob
= LoadResource(wps
->hMod
, hRes
)) == 0)
262 if ((data
= LockResource(hGlob
)) == NULL
) {
268 data
= (void*)wps
->pszSound
;
270 /* construct an MMIO stream (either in memory, or from a file */
271 if (wps
->fdwSound
& SND_MEMORY
)
272 { /* NOTE: SND_RESOURCE has the SND_MEMORY bit set */
275 memset(&mminfo
, 0, sizeof(mminfo
));
276 mminfo
.fccIOProc
= FOURCC_MEM
;
277 mminfo
.pchBuffer
= (LPSTR
)data
;
278 mminfo
.cchBuffer
= -1; /* FIXME: when a resource, could grab real size */
279 TRACE("Memory sound %p\n", data
);
280 hmmio
= mmioOpenW(NULL
, &mminfo
, MMIO_READ
);
282 else if (wps
->fdwSound
& SND_ALIAS
)
284 hmmio
= get_mmioFromProfile(wps
->fdwSound
, wps
->pszSound
);
286 else if (wps
->fdwSound
& SND_FILENAME
)
288 hmmio
= get_mmioFromFile(wps
->pszSound
);
292 if ((hmmio
= get_mmioFromProfile(wps
->fdwSound
| SND_NODEFAULT
, wps
->pszSound
)) == 0)
294 if ((hmmio
= get_mmioFromFile(wps
->pszSound
)) == 0)
296 hmmio
= get_mmioFromProfile(wps
->fdwSound
, wps
->pszSound
);
300 if (hmmio
== 0) goto errCleanUp
;
302 if (mmioDescend(hmmio
, &ckMainRIFF
, NULL
, 0))
305 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08lX \n",
306 (LPSTR
)&ckMainRIFF
.ckid
, (LPSTR
)&ckMainRIFF
.fccType
, ckMainRIFF
.cksize
);
308 if ((ckMainRIFF
.ckid
!= FOURCC_RIFF
) ||
309 (ckMainRIFF
.fccType
!= mmioFOURCC('W', 'A', 'V', 'E')))
312 mmckInfo
.ckid
= mmioFOURCC('f', 'm', 't', ' ');
313 if (mmioDescend(hmmio
, &mmckInfo
, &ckMainRIFF
, MMIO_FINDCHUNK
))
316 TRACE("Chunk Found ckid=%.4s fccType=%.4s cksize=%08lX \n",
317 (LPSTR
)&mmckInfo
.ckid
, (LPSTR
)&mmckInfo
.fccType
, mmckInfo
.cksize
);
319 lpWaveFormat
= HeapAlloc(GetProcessHeap(), 0, mmckInfo
.cksize
);
320 if (mmioRead(hmmio
, (HPSTR
)lpWaveFormat
, mmckInfo
.cksize
) < sizeof(WAVEFORMAT
))
323 TRACE("wFormatTag=%04X !\n", lpWaveFormat
->wFormatTag
);
324 TRACE("nChannels=%d \n", lpWaveFormat
->nChannels
);
325 TRACE("nSamplesPerSec=%ld\n", lpWaveFormat
->nSamplesPerSec
);
326 TRACE("nAvgBytesPerSec=%ld\n", lpWaveFormat
->nAvgBytesPerSec
);
327 TRACE("nBlockAlign=%d \n", lpWaveFormat
->nBlockAlign
);
328 TRACE("wBitsPerSample=%u !\n", lpWaveFormat
->wBitsPerSample
);
330 /* move to end of 'fmt ' chunk */
331 mmioAscend(hmmio
, &mmckInfo
, 0);
333 mmckInfo
.ckid
= mmioFOURCC('d', 'a', 't', 'a');
334 if (mmioDescend(hmmio
, &mmckInfo
, &ckMainRIFF
, MMIO_FINDCHUNK
))
337 TRACE("Chunk Found ckid=%.4s fccType=%.4s cksize=%08lX\n",
338 (LPSTR
)&mmckInfo
.ckid
, (LPSTR
)&mmckInfo
.fccType
, mmckInfo
.cksize
);
340 s
.hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
342 if (waveOutOpen(&hWave
, WAVE_MAPPER
, lpWaveFormat
, (DWORD
)PlaySound_Callback
,
343 (DWORD
)&s
, CALLBACK_FUNCTION
) != MMSYSERR_NOERROR
)
346 /* make it so that 3 buffers per second are needed */
347 bufsize
= (((lpWaveFormat
->nAvgBytesPerSec
/ 3) - 1) / lpWaveFormat
->nBlockAlign
+ 1) *
348 lpWaveFormat
->nBlockAlign
;
349 waveHdr
= HeapAlloc(GetProcessHeap(), 0, 2 * sizeof(WAVEHDR
) + 2 * bufsize
);
350 waveHdr
[0].lpData
= (char*)waveHdr
+ 2 * sizeof(WAVEHDR
);
351 waveHdr
[1].lpData
= (char*)waveHdr
+ 2 * sizeof(WAVEHDR
) + bufsize
;
352 waveHdr
[0].dwUser
= waveHdr
[1].dwUser
= 0L;
353 waveHdr
[0].dwLoops
= waveHdr
[1].dwLoops
= 0L;
354 waveHdr
[0].dwFlags
= waveHdr
[1].dwFlags
= 0L;
355 waveHdr
[0].dwBufferLength
= waveHdr
[1].dwBufferLength
= bufsize
;
356 if (waveOutPrepareHeader(hWave
, &waveHdr
[0], sizeof(WAVEHDR
)) ||
357 waveOutPrepareHeader(hWave
, &waveHdr
[1], sizeof(WAVEHDR
))) {
361 s
.dwEventCount
= 1L; /* for first buffer */
365 left
= mmckInfo
.cksize
;
367 mmioSeek(hmmio
, mmckInfo
.dwDataOffset
, SEEK_SET
);
370 if (WaitForSingleObject(WINMM_IData
->psStopEvent
, 0) == WAIT_OBJECT_0
)
375 count
= mmioRead(hmmio
, waveHdr
[index
].lpData
, min(bufsize
, left
));
376 if (count
< 1) break;
378 waveHdr
[index
].dwBufferLength
= count
;
379 waveHdr
[index
].dwFlags
&= ~WHDR_DONE
;
380 if (waveOutWrite(hWave
, &waveHdr
[index
], sizeof(WAVEHDR
)) == MMSYSERR_NOERROR
) {
382 PlaySound_WaitDone(&s
);
384 else FIXME("Couldn't play header\n");
387 } while (wps
->bLoop
);
389 PlaySound_WaitDone(&s
); /* for last buffer */
392 waveOutUnprepareHeader(hWave
, &waveHdr
[0], sizeof(WAVEHDR
));
393 waveOutUnprepareHeader(hWave
, &waveHdr
[1], sizeof(WAVEHDR
));
396 TRACE("Done playing='%s' => %s!\n", debugstr_w(wps
->pszSound
), bRet
? "ok" : "ko");
397 CloseHandle(s
.hEvent
);
398 if (waveHdr
) HeapFree(GetProcessHeap(), 0, waveHdr
);
399 if (lpWaveFormat
) HeapFree(GetProcessHeap(), 0, lpWaveFormat
);
400 if (hWave
) while (waveOutClose(hWave
) == WAVERR_STILLPLAYING
) Sleep(100);
401 if (hmmio
) mmioClose(hmmio
, 0);
408 BOOL
MULTIMEDIA_PlaySound(const void* pszSound
, HMODULE hmod
, DWORD fdwSound
, BOOL bUnicode
)
410 WINE_PLAYSOUND
* wps
= NULL
;
412 TRACE("pszSound='%p' hmod=%p fdwSound=%08lX\n",
413 pszSound
, hmod
, fdwSound
);
415 /* FIXME? I see no difference between SND_NOWAIT and SND_NOSTOP !
416 * there could be one if several sounds can be played at once...
418 if ((fdwSound
& (SND_NOWAIT
| SND_NOSTOP
)) && WINMM_IData
->lpPlaySound
!= NULL
)
421 /* alloc internal structure, if we need to play something */
422 if (pszSound
&& !(fdwSound
& SND_PURGE
))
424 if (!(wps
= PlaySound_Alloc(pszSound
, hmod
, fdwSound
, bUnicode
)))
428 EnterCriticalSection(&WINMM_IData
->cs
);
429 /* since several threads can enter PlaySound in parallel, we're not
430 * sure, at this point, that another thread didn't start a new playsound
432 while (WINMM_IData
->lpPlaySound
!= NULL
)
434 ResetEvent(WINMM_IData
->psLastEvent
);
435 /* FIXME: doc says we have to stop all instances of pszSound if it's non
436 * NULL... as of today, we stop all playing instances */
437 SetEvent(WINMM_IData
->psStopEvent
);
439 LeaveCriticalSection(&WINMM_IData
->cs
);
440 WaitForSingleObject(WINMM_IData
->psLastEvent
, INFINITE
);
441 EnterCriticalSection(&WINMM_IData
->cs
);
443 ResetEvent(WINMM_IData
->psStopEvent
);
446 if (wps
) wps
->lpNext
= WINMM_IData
->lpPlaySound
;
447 WINMM_IData
->lpPlaySound
= wps
;
448 LeaveCriticalSection(&WINMM_IData
->cs
);
450 if (!pszSound
|| (fdwSound
& SND_PURGE
)) return TRUE
;
452 if (fdwSound
& SND_ASYNC
)
455 wps
->bLoop
= (fdwSound
& SND_LOOP
) ? TRUE
: FALSE
;
456 if (CreateThread(NULL
, 0, proc_PlaySound
, wps
, 0, &id
) != 0)
459 else return proc_PlaySound(wps
);
466 /**************************************************************************
467 * PlaySoundA [WINMM.@]
469 BOOL WINAPI
PlaySoundA(LPCSTR pszSoundA
, HMODULE hmod
, DWORD fdwSound
)
471 return MULTIMEDIA_PlaySound(pszSoundA
, hmod
, fdwSound
, FALSE
);
474 /**************************************************************************
475 * PlaySoundW [WINMM.@]
477 BOOL WINAPI
PlaySoundW(LPCWSTR pszSoundW
, HMODULE hmod
, DWORD fdwSound
)
479 return MULTIMEDIA_PlaySound(pszSoundW
, hmod
, fdwSound
, TRUE
);
482 /**************************************************************************
483 * sndPlaySoundA [WINMM.@]
485 BOOL WINAPI
sndPlaySoundA(LPCSTR pszSoundA
, UINT uFlags
)
487 uFlags
&= SND_ASYNC
|SND_LOOP
|SND_MEMORY
|SND_NODEFAULT
|SND_NOSTOP
|SND_SYNC
;
488 return MULTIMEDIA_PlaySound(pszSoundA
, 0, uFlags
, FALSE
);
491 /**************************************************************************
492 * sndPlaySoundW [WINMM.@]
494 BOOL WINAPI
sndPlaySoundW(LPCWSTR pszSound
, UINT uFlags
)
496 uFlags
&= SND_ASYNC
|SND_LOOP
|SND_MEMORY
|SND_NODEFAULT
|SND_NOSTOP
|SND_SYNC
;
497 return MULTIMEDIA_PlaySound(pszSound
, 0, uFlags
, TRUE
);
500 /**************************************************************************
501 * mmsystemGetVersion [WINMM.@]
503 UINT WINAPI
mmsystemGetVersion(void)
505 TRACE("3.10 (Win95?)\n");