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
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
40 static HMMIO get_mmioFromFile(LPCWSTR lpszName)
46 ret = mmioOpenW((LPWSTR)lpszName, NULL,
47 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
48 if (ret != 0) return ret;
49 if (SearchPathW(NULL, lpszName, NULL, sizeof(buf)/sizeof(buf[0]), buf, &dummy))
51 return mmioOpenW(buf, NULL,
52 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
57 static HMMIO get_mmioFromProfile(UINT uFlags, LPCWSTR lpszName)
62 HKEY hRegSnd, hRegApp, hScheme, hSnd;
63 DWORD err, type, count;
65 static WCHAR wszSounds[] = {'S','o','u','n','d','s',0};
66 static WCHAR wszDefault[] = {'D','e','f','a','u','l','t',0};
67 static WCHAR wszKey[] = {'A','p','p','E','v','e','n','t','s','\\',
68 'S','c','h','e','m','e','s','\\',
70 static WCHAR wszDotDefault[] = {'.','D','e','f','a','u','l','t',0};
71 static WCHAR wszNull[] = {0};
73 TRACE("searching in SystemSound list for %s\n", debugstr_w(lpszName));
74 GetProfileStringW(wszSounds, (LPWSTR)lpszName, wszNull, str, sizeof(str)/sizeof(str[0]));
75 if (lstrlenW(str) == 0)
77 if (uFlags & SND_NODEFAULT) goto next;
78 GetProfileStringW(wszSounds, wszDefault, wszNull, str, sizeof(str)/sizeof(str[0]));
79 if (lstrlenW(str) == 0) goto next;
81 for (ptr = str; *ptr && *ptr != ','; ptr++);
83 hmmio = mmioOpenW(str, NULL, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
84 if (hmmio != 0) return hmmio;
86 /* we look up the registry under
87 * HKCU\AppEvents\Schemes\Apps\.Default
88 * HKCU\AppEvents\Schemes\Apps\<AppName>
90 if (RegOpenKeyW(HKEY_CURRENT_USER, wszKey, &hRegSnd) != 0) goto none;
91 if (uFlags & SND_APPLICATION)
94 if (GetModuleFileNameW(0, str, sizeof(str)/sizeof(str[0])))
96 for (ptr = str + lstrlenW(str) - 1; ptr >= str; ptr--)
98 if (*ptr == '.') *ptr = 0;
101 err = RegOpenKeyW(hRegSnd, str, &hRegApp);
109 err = RegOpenKeyW(hRegSnd, wszDotDefault, &hRegApp);
111 RegCloseKey(hRegSnd);
112 if (err != 0) goto none;
113 err = RegOpenKeyW(hRegApp, lpszName, &hScheme);
114 RegCloseKey(hRegApp);
115 if (err != 0) goto none;
116 err = RegOpenKeyW(hScheme, wszDotDefault, &hSnd);
117 RegCloseKey(hScheme);
118 if (err != 0) goto none;
119 count = sizeof(str)/sizeof(str[0]);
120 err = RegQueryValueExW(hSnd, NULL, 0, &type, (LPBYTE)str, &count);
122 if (err != 0 || !*str) goto none;
123 hmmio = mmioOpenW(str, NULL, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
124 if (hmmio) return hmmio;
126 WARN("can't find SystemSound='%s' !\n", debugstr_w(lpszName));
130 struct playsound_data
136 static void CALLBACK PlaySound_Callback(HWAVEOUT hwo, UINT uMsg,
138 DWORD dwParam1, DWORD dwParam2)
140 struct playsound_data* s = (struct playsound_data*)dwInstance;
147 InterlockedIncrement(&s->dwEventCount);
148 TRACE("Returning waveHdr=%lx\n", dwParam1);
152 ERR("Unknown uMsg=%d\n", uMsg);
156 static void PlaySound_WaitDone(struct playsound_data* s)
159 ResetEvent(s->hEvent);
160 if (InterlockedDecrement(&s->dwEventCount) >= 0) break;
161 InterlockedIncrement(&s->dwEventCount);
163 WaitForSingleObject(s->hEvent, INFINITE);
167 static BOOL PlaySound_IsString(DWORD fdwSound, const void* psz)
169 /* SND_RESOURCE is 0x40004 while
170 * SND_MEMORY is 0x00004
172 switch (fdwSound & (SND_RESOURCE|SND_ALIAS|SND_FILENAME))
174 case SND_RESOURCE: return HIWORD(psz) != 0; /* by name or by ID ? */
175 case SND_MEMORY: return FALSE;
176 case SND_ALIAS: /* what about ALIAS_ID ??? */
179 default: FIXME("WTF\n"); return FALSE;
183 static void PlaySound_Free(WINE_PLAYSOUND* wps)
187 EnterCriticalSection(&WINMM_IData->cs);
188 for (p = &WINMM_IData->lpPlaySound; *p && *p != wps; p = &((*p)->lpNext));
189 if (*p) *p = (*p)->lpNext;
190 if (WINMM_IData->lpPlaySound == NULL) SetEvent(WINMM_IData->psLastEvent);
191 LeaveCriticalSection(&WINMM_IData->cs);
192 if (wps->bAlloc) HeapFree(GetProcessHeap(), 0, (void*)wps->pszSound);
193 HeapFree(GetProcessHeap(), 0, wps);
196 static WINE_PLAYSOUND* PlaySound_Alloc(const void* pszSound, HMODULE hmod,
197 DWORD fdwSound, BOOL bUnicode)
201 wps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wps));
202 if (!wps) return NULL;
205 wps->fdwSound = fdwSound;
206 if (PlaySound_IsString(fdwSound, pszSound))
210 if (fdwSound & SND_ASYNC)
212 wps->pszSound = HeapAlloc(GetProcessHeap(), 0,
213 (lstrlenW(pszSound)+1) * sizeof(WCHAR));
214 if (!wps->pszSound) goto oom_error;
215 lstrcpyW((LPWSTR)wps->pszSound, pszSound);
219 wps->pszSound = pszSound;
223 UNICODE_STRING usBuffer;
224 RtlCreateUnicodeStringFromAsciiz(&usBuffer, pszSound);
225 wps->pszSound = usBuffer.Buffer;
226 if (!wps->pszSound) goto oom_error;
231 wps->pszSound = pszSound;
239 static DWORD WINAPI proc_PlaySound(LPVOID arg)
241 WINE_PLAYSOUND* wps = (WINE_PLAYSOUND*)arg;
246 LPWAVEFORMATEX lpWaveFormat = NULL;
248 LPWAVEHDR waveHdr = NULL;
249 INT count, bufsize, left, index;
250 struct playsound_data s;
255 TRACE("SoundName='%s' !\n", debugstr_w(wps->pszSound));
257 /* if resource, grab it */
258 if ((wps->fdwSound & SND_RESOURCE) == SND_RESOURCE) {
259 static WCHAR wszWave[] = {'W','A','V','E',0};
263 if ((hRes = FindResourceW(wps->hMod, wps->pszSound, wszWave)) == 0 ||
264 (hGlob = LoadResource(wps->hMod, hRes)) == 0)
266 if ((data = LockResource(hGlob)) == NULL) {
272 data = (void*)wps->pszSound;
274 /* construct an MMIO stream (either in memory, or from a file */
275 if (wps->fdwSound & SND_MEMORY)
276 { /* NOTE: SND_RESOURCE has the SND_MEMORY bit set */
279 memset(&mminfo, 0, sizeof(mminfo));
280 mminfo.fccIOProc = FOURCC_MEM;
281 mminfo.pchBuffer = (LPSTR)data;
282 mminfo.cchBuffer = -1; /* FIXME: when a resource, could grab real size */
283 TRACE("Memory sound %p\n", data);
284 hmmio = mmioOpenW(NULL, &mminfo, MMIO_READ);
286 else if (wps->fdwSound & SND_ALIAS)
288 hmmio = get_mmioFromProfile(wps->fdwSound, wps->pszSound);
290 else if (wps->fdwSound & SND_FILENAME)
292 hmmio = get_mmioFromFile(wps->pszSound);
296 if ((hmmio = get_mmioFromProfile(wps->fdwSound | SND_NODEFAULT, wps->pszSound)) == 0)
298 if ((hmmio = get_mmioFromFile(wps->pszSound)) == 0)
300 hmmio = get_mmioFromProfile(wps->fdwSound, wps->pszSound);
304 if (hmmio == 0) goto errCleanUp;
306 if (mmioDescend(hmmio, &ckMainRIFF, NULL, 0))
309 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08lX \n",
310 (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, ckMainRIFF.cksize);
312 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
313 (ckMainRIFF.fccType != mmioFOURCC('W', 'A', 'V', 'E')))
316 mmckInfo.ckid = mmioFOURCC('f', 'm', 't', ' ');
317 if (mmioDescend(hmmio, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK))
320 TRACE("Chunk Found ckid=%.4s fccType=%.4s cksize=%08lX \n",
321 (LPSTR)&mmckInfo.ckid, (LPSTR)&mmckInfo.fccType, mmckInfo.cksize);
323 lpWaveFormat = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
324 if (mmioRead(hmmio, (HPSTR)lpWaveFormat, mmckInfo.cksize) < sizeof(WAVEFORMAT))
327 TRACE("wFormatTag=%04X !\n", lpWaveFormat->wFormatTag);
328 TRACE("nChannels=%d \n", lpWaveFormat->nChannels);
329 TRACE("nSamplesPerSec=%ld\n", lpWaveFormat->nSamplesPerSec);
330 TRACE("nAvgBytesPerSec=%ld\n", lpWaveFormat->nAvgBytesPerSec);
331 TRACE("nBlockAlign=%d \n", lpWaveFormat->nBlockAlign);
332 TRACE("wBitsPerSample=%u !\n", lpWaveFormat->wBitsPerSample);
334 /* move to end of 'fmt ' chunk */
335 mmioAscend(hmmio, &mmckInfo, 0);
337 mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
338 if (mmioDescend(hmmio, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK))
341 TRACE("Chunk Found ckid=%.4s fccType=%.4s cksize=%08lX\n",
342 (LPSTR)&mmckInfo.ckid, (LPSTR)&mmckInfo.fccType, mmckInfo.cksize);
344 s.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
346 if (waveOutOpen(&hWave, WAVE_MAPPER, lpWaveFormat, (DWORD)PlaySound_Callback,
347 (DWORD)&s, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
350 /* make it so that 3 buffers per second are needed */
351 bufsize = (((lpWaveFormat->nAvgBytesPerSec / 3) - 1) / lpWaveFormat->nBlockAlign + 1) *
352 lpWaveFormat->nBlockAlign;
353 waveHdr = HeapAlloc(GetProcessHeap(), 0, 2 * sizeof(WAVEHDR) + 2 * bufsize);
354 waveHdr[0].lpData = (char*)waveHdr + 2 * sizeof(WAVEHDR);
355 waveHdr[1].lpData = (char*)waveHdr + 2 * sizeof(WAVEHDR) + bufsize;
356 waveHdr[0].dwUser = waveHdr[1].dwUser = 0L;
357 waveHdr[0].dwLoops = waveHdr[1].dwLoops = 0L;
358 waveHdr[0].dwFlags = waveHdr[1].dwFlags = 0L;
359 waveHdr[0].dwBufferLength = waveHdr[1].dwBufferLength = bufsize;
360 if (waveOutPrepareHeader(hWave, &waveHdr[0], sizeof(WAVEHDR)) ||
361 waveOutPrepareHeader(hWave, &waveHdr[1], sizeof(WAVEHDR))) {
365 s.dwEventCount = 1L; /* for first buffer */
369 left = mmckInfo.cksize;
371 mmioSeek(hmmio, mmckInfo.dwDataOffset, SEEK_SET);
374 if (WaitForSingleObject(WINMM_IData->psStopEvent, 0) == WAIT_OBJECT_0)
379 count = mmioRead(hmmio, waveHdr[index].lpData, min(bufsize, left));
380 if (count < 1) break;
382 waveHdr[index].dwBufferLength = count;
383 waveHdr[index].dwFlags &= ~WHDR_DONE;
384 if (waveOutWrite(hWave, &waveHdr[index], sizeof(WAVEHDR)) == MMSYSERR_NOERROR) {
386 PlaySound_WaitDone(&s);
388 else FIXME("Couldn't play header\n");
391 } while (wps->bLoop);
393 PlaySound_WaitDone(&s); /* for last buffer */
396 waveOutUnprepareHeader(hWave, &waveHdr[0], sizeof(WAVEHDR));
397 waveOutUnprepareHeader(hWave, &waveHdr[1], sizeof(WAVEHDR));
400 TRACE("Done playing='%s' => %s!\n", debugstr_w(wps->pszSound), bRet ? "ok" : "ko");
401 CloseHandle(s.hEvent);
402 if (waveHdr) HeapFree(GetProcessHeap(), 0, waveHdr);
403 if (lpWaveFormat) HeapFree(GetProcessHeap(), 0, lpWaveFormat);
404 if (hWave) while (waveOutClose(hWave) == WAVERR_STILLPLAYING) Sleep(100);
405 if (hmmio) mmioClose(hmmio, 0);
412 BOOL MULTIMEDIA_PlaySound(const void* pszSound, HMODULE hmod, DWORD fdwSound, BOOL bUnicode)
414 WINE_PLAYSOUND* wps = NULL;
416 TRACE("pszSound='%p' hmod=%p fdwSound=%08lX\n",
417 pszSound, hmod, fdwSound);
419 /* FIXME? I see no difference between SND_NOWAIT and SND_NOSTOP !
420 * there could be one if several sounds can be played at once...
422 if ((fdwSound & (SND_NOWAIT | SND_NOSTOP)) && WINMM_IData->lpPlaySound != NULL)
425 /* alloc internal structure, if we need to play something */
426 if (pszSound && !(fdwSound & SND_PURGE))
428 if (!(wps = PlaySound_Alloc(pszSound, hmod, fdwSound, bUnicode)))
432 EnterCriticalSection(&WINMM_IData->cs);
433 /* since several threads can enter PlaySound in parallel, we're not
434 * sure, at this point, that another thread didn't start a new playsound
436 while (WINMM_IData->lpPlaySound != NULL)
438 ResetEvent(WINMM_IData->psLastEvent);
439 /* FIXME: doc says we have to stop all instances of pszSound if it's non
440 * NULL... as of today, we stop all playing instances */
441 SetEvent(WINMM_IData->psStopEvent);
443 LeaveCriticalSection(&WINMM_IData->cs);
444 WaitForSingleObject(WINMM_IData->psLastEvent, INFINITE);
445 EnterCriticalSection(&WINMM_IData->cs);
447 ResetEvent(WINMM_IData->psStopEvent);
450 if (wps) wps->lpNext = WINMM_IData->lpPlaySound;
451 WINMM_IData->lpPlaySound = wps;
452 LeaveCriticalSection(&WINMM_IData->cs);
454 if (!pszSound || (fdwSound & SND_PURGE)) return TRUE;
456 if (fdwSound & SND_ASYNC)
459 wps->bLoop = (fdwSound & SND_LOOP) ? TRUE : FALSE;
460 if (CreateThread(NULL, 0, proc_PlaySound, wps, 0, &id) != 0)
463 else return proc_PlaySound(wps);
470 /**************************************************************************
471 * PlaySoundA [WINMM.@]
473 BOOL WINAPI PlaySoundA(LPCSTR pszSoundA, HMODULE hmod, DWORD fdwSound)
475 return MULTIMEDIA_PlaySound(pszSoundA, hmod, fdwSound, FALSE);
478 /**************************************************************************
479 * PlaySoundW [WINMM.@]
481 BOOL WINAPI PlaySoundW(LPCWSTR pszSoundW, HMODULE hmod, DWORD fdwSound)
483 return MULTIMEDIA_PlaySound(pszSoundW, hmod, fdwSound, TRUE);
486 /**************************************************************************
487 * sndPlaySoundA [WINMM.@]
489 BOOL WINAPI sndPlaySoundA(LPCSTR pszSoundA, UINT uFlags)
491 uFlags &= SND_ASYNC|SND_LOOP|SND_MEMORY|SND_NODEFAULT|SND_NOSTOP|SND_SYNC;
492 return MULTIMEDIA_PlaySound(pszSoundA, 0, uFlags, FALSE);
495 /**************************************************************************
496 * sndPlaySoundW [WINMM.@]
498 BOOL WINAPI sndPlaySoundW(LPCWSTR pszSound, UINT uFlags)
500 uFlags &= SND_ASYNC|SND_LOOP|SND_MEMORY|SND_NODEFAULT|SND_NOSTOP|SND_SYNC;
501 return MULTIMEDIA_PlaySound(pszSound, 0, uFlags, TRUE);
504 /**************************************************************************
505 * mmsystemGetVersion [WINMM.@]
507 UINT WINAPI mmsystemGetVersion(void)
509 TRACE("3.10 (Win95?)\n");