Removed calls to HEAP_strdupAtoW.
[wine/multimedia.git] / dlls / winmm / playsound.c
blob52ffca163874eebe37ffe23103f25af1e89efdaf
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * MMSYTEM functions
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
24 #include <string.h>
26 #include "mmsystem.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "heap.h"
31 #include "winreg.h"
32 #include "winemm.h"
33 #include "winternl.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
39 static HMMIO get_mmioFromFile(LPCWSTR lpszName)
41 HMMIO ret;
42 WCHAR buf[256];
43 LPWSTR dummy;
45 ret = mmioOpenW((LPWSTR)lpszName, NULL,
46 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
47 if (ret != 0) return ret;
48 if (SearchPathW(NULL, lpszName, NULL, sizeof(buf)/sizeof(buf[0]), buf, &dummy))
50 return mmioOpenW(buf, NULL,
51 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
53 return 0;
56 static HMMIO get_mmioFromProfile(UINT uFlags, LPCWSTR lpszName)
58 WCHAR str[128];
59 LPWSTR ptr;
60 HMMIO hmmio;
61 HKEY hRegSnd, hRegApp, hScheme, hSnd;
62 DWORD err, type, count;
64 static WCHAR wszSounds[] = {'S','o','u','n','d','s',0};
65 static WCHAR wszDefault[] = {'D','e','f','a','u','l','t',0};
66 static WCHAR wszKey[] = {'A','p','p','E','v','e','n','t','s','\\',
67 'S','c','h','e','m','e','s','\\',
68 'A','p','p','s',0};
69 static WCHAR wszDotDefault[] = {'.','D','e','f','a','u','l','t',0};
70 static WCHAR wszNull[] = {0};
72 TRACE("searching in SystemSound list for %s\n", debugstr_w(lpszName));
73 GetProfileStringW(wszSounds, (LPWSTR)lpszName, wszNull, str, sizeof(str)/sizeof(str[0]));
74 if (lstrlenW(str) == 0)
76 if (uFlags & SND_NODEFAULT) goto next;
77 GetProfileStringW(wszSounds, wszDefault, wszNull, str, sizeof(str)/sizeof(str[0]));
78 if (lstrlenW(str) == 0) goto next;
80 for (ptr = str; *ptr && *ptr != ','; ptr++);
81 if (*ptr) *ptr = 0;
82 hmmio = mmioOpenW(str, NULL, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
83 if (hmmio != 0) return hmmio;
84 next:
85 /* we look up the registry under
86 * HKCU\AppEvents\Schemes\Apps\.Default
87 * HKCU\AppEvents\Schemes\Apps\<AppName>
89 if (RegOpenKeyW(HKEY_CURRENT_USER, wszKey, &hRegSnd) != 0) goto none;
90 if (uFlags & SND_APPLICATION)
92 err = 1; /* error */
93 if (GetModuleFileNameW(0, str, sizeof(str)/sizeof(str[0])))
95 for (ptr = str + lstrlenW(str) - 1; ptr >= str; ptr--)
97 if (*ptr == '.') *ptr = 0;
98 if (*ptr == '\\')
100 err = RegOpenKeyW(hRegSnd, str, &hRegApp);
101 break;
106 else
108 err = RegOpenKeyW(hRegSnd, wszDotDefault, &hRegApp);
110 RegCloseKey(hRegSnd);
111 if (err != 0) goto none;
112 err = RegOpenKeyW(hRegApp, lpszName, &hScheme);
113 RegCloseKey(hRegApp);
114 if (err != 0) goto none;
115 err = RegOpenKeyW(hScheme, wszDotDefault, &hSnd);
116 RegCloseKey(hScheme);
117 if (err != 0) goto none;
118 count = sizeof(str)/sizeof(str[0]);
119 err = RegQueryValueExW(hSnd, NULL, 0, &type, (LPBYTE)str, &count);
120 RegCloseKey(hSnd);
121 if (err != 0 || !*str) goto none;
122 hmmio = mmioOpenW(str, NULL, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
123 if (hmmio) return hmmio;
124 none:
125 WARN("can't find SystemSound='%s' !\n", debugstr_w(lpszName));
126 return 0;
129 struct playsound_data
131 HANDLE hEvent;
132 DWORD dwEventCount;
135 static void CALLBACK PlaySound_Callback(HWAVEOUT hwo, UINT uMsg,
136 DWORD dwInstance,
137 DWORD dwParam1, DWORD dwParam2)
139 struct playsound_data* s = (struct playsound_data*)dwInstance;
141 switch (uMsg) {
142 case WOM_OPEN:
143 case WOM_CLOSE:
144 break;
145 case WOM_DONE:
146 InterlockedIncrement(&s->dwEventCount);
147 TRACE("Returning waveHdr=%lx\n", dwParam1);
148 SetEvent(s->hEvent);
149 break;
150 default:
151 ERR("Unknown uMsg=%d\n", uMsg);
155 static void PlaySound_WaitDone(struct playsound_data* s)
157 for (;;) {
158 ResetEvent(s->hEvent);
159 if (InterlockedDecrement(&s->dwEventCount) >= 0) break;
160 InterlockedIncrement(&s->dwEventCount);
162 WaitForSingleObject(s->hEvent, INFINITE);
166 static BOOL PlaySound_IsString(DWORD fdwSound, const void* psz)
168 /* SND_RESOURCE is 0x40004 while
169 * SND_MEMORY is 0x00004
171 switch (fdwSound & (SND_RESOURCE|SND_ALIAS|SND_FILENAME))
173 case SND_RESOURCE: return HIWORD(psz) != 0; /* by name or by ID ? */
174 case SND_MEMORY: return FALSE;
175 case SND_ALIAS: /* what about ALIAS_ID ??? */
176 case SND_FILENAME:
177 case 0: return TRUE;
178 default: FIXME("WTF\n"); return FALSE;
182 static void PlaySound_Free(WINE_PLAYSOUND* wps)
184 WINE_PLAYSOUND** p;
186 EnterCriticalSection(&WINMM_IData->cs);
187 for (p = &WINMM_IData->lpPlaySound; *p && *p != wps; p = &((*p)->lpNext));
188 if (*p) *p = (*p)->lpNext;
189 if (WINMM_IData->lpPlaySound == NULL) SetEvent(WINMM_IData->psLastEvent);
190 LeaveCriticalSection(&WINMM_IData->cs);
191 if (wps->bAlloc) HeapFree(GetProcessHeap(), 0, (void*)wps->pszSound);
192 HeapFree(GetProcessHeap(), 0, wps);
195 static WINE_PLAYSOUND* PlaySound_Alloc(const void* pszSound, HMODULE hmod,
196 DWORD fdwSound, BOOL bUnicode)
198 WINE_PLAYSOUND* wps;
200 wps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wps));
201 if (!wps) return NULL;
203 wps->hMod = hmod;
204 wps->fdwSound = fdwSound;
205 if (PlaySound_IsString(fdwSound, pszSound))
207 if (bUnicode)
209 if (fdwSound & SND_ASYNC)
211 wps->pszSound = HeapAlloc(GetProcessHeap(), 0,
212 (lstrlenW(pszSound)+1) * sizeof(WCHAR));
213 if (!wps->pszSound) goto oom_error;
214 lstrcpyW((LPWSTR)wps->pszSound, pszSound);
215 wps->bAlloc = TRUE;
217 else
218 wps->pszSound = pszSound;
220 else
222 UNICODE_STRING usBuffer;
223 RtlCreateUnicodeStringFromAsciiz(&usBuffer, pszSound);
224 wps->pszSound = usBuffer.Buffer;
225 if (!wps->pszSound) goto oom_error;
226 wps->bAlloc = TRUE;
229 else
230 wps->pszSound = pszSound;
232 return wps;
233 oom_error:
234 PlaySound_Free(wps);
235 return NULL;
238 static DWORD WINAPI proc_PlaySound(LPVOID arg)
240 WINE_PLAYSOUND* wps = (WINE_PLAYSOUND*)arg;
241 BOOL bRet = FALSE;
242 HMMIO hmmio = 0;
243 MMCKINFO ckMainRIFF;
244 MMCKINFO mmckInfo;
245 LPWAVEFORMATEX lpWaveFormat = NULL;
246 HWAVEOUT hWave = 0;
247 LPWAVEHDR waveHdr = NULL;
248 INT count, bufsize, left, index;
249 struct playsound_data s;
250 void* data;
252 s.hEvent = 0;
254 TRACE("SoundName='%s' !\n", debugstr_w(wps->pszSound));
256 /* if resource, grab it */
257 if ((wps->fdwSound & SND_RESOURCE) == SND_RESOURCE) {
258 static WCHAR wszWave[] = {'W','A','V','E',0};
259 HRSRC hRes;
260 HGLOBAL hGlob;
262 if ((hRes = FindResourceW(wps->hMod, wps->pszSound, wszWave)) == 0 ||
263 (hGlob = LoadResource(wps->hMod, hRes)) == 0)
264 goto errCleanUp;
265 if ((data = LockResource(hGlob)) == NULL) {
266 FreeResource(hGlob);
267 goto errCleanUp;
269 FreeResource(hGlob);
270 } else
271 data = (void*)wps->pszSound;
273 /* construct an MMIO stream (either in memory, or from a file */
274 if (wps->fdwSound & SND_MEMORY)
275 { /* NOTE: SND_RESOURCE has the SND_MEMORY bit set */
276 MMIOINFO mminfo;
278 memset(&mminfo, 0, sizeof(mminfo));
279 mminfo.fccIOProc = FOURCC_MEM;
280 mminfo.pchBuffer = (LPSTR)data;
281 mminfo.cchBuffer = -1; /* FIXME: when a resource, could grab real size */
282 TRACE("Memory sound %p\n", data);
283 hmmio = mmioOpenW(NULL, &mminfo, MMIO_READ);
285 else if (wps->fdwSound & SND_ALIAS)
287 hmmio = get_mmioFromProfile(wps->fdwSound, wps->pszSound);
289 else if (wps->fdwSound & SND_FILENAME)
291 hmmio = get_mmioFromFile(wps->pszSound);
293 else
295 if ((hmmio = get_mmioFromProfile(wps->fdwSound | SND_NODEFAULT, wps->pszSound)) == 0)
297 if ((hmmio = get_mmioFromFile(wps->pszSound)) == 0)
299 hmmio = get_mmioFromProfile(wps->fdwSound, wps->pszSound);
303 if (hmmio == 0) goto errCleanUp;
305 if (mmioDescend(hmmio, &ckMainRIFF, NULL, 0))
306 goto errCleanUp;
308 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08lX \n",
309 (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, ckMainRIFF.cksize);
311 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
312 (ckMainRIFF.fccType != mmioFOURCC('W', 'A', 'V', 'E')))
313 goto errCleanUp;
315 mmckInfo.ckid = mmioFOURCC('f', 'm', 't', ' ');
316 if (mmioDescend(hmmio, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK))
317 goto errCleanUp;
319 TRACE("Chunk Found ckid=%.4s fccType=%.4s cksize=%08lX \n",
320 (LPSTR)&mmckInfo.ckid, (LPSTR)&mmckInfo.fccType, mmckInfo.cksize);
322 lpWaveFormat = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
323 if (mmioRead(hmmio, (HPSTR)lpWaveFormat, mmckInfo.cksize) < sizeof(WAVEFORMAT))
324 goto errCleanUp;
326 TRACE("wFormatTag=%04X !\n", lpWaveFormat->wFormatTag);
327 TRACE("nChannels=%d \n", lpWaveFormat->nChannels);
328 TRACE("nSamplesPerSec=%ld\n", lpWaveFormat->nSamplesPerSec);
329 TRACE("nAvgBytesPerSec=%ld\n", lpWaveFormat->nAvgBytesPerSec);
330 TRACE("nBlockAlign=%d \n", lpWaveFormat->nBlockAlign);
331 TRACE("wBitsPerSample=%u !\n", lpWaveFormat->wBitsPerSample);
333 /* move to end of 'fmt ' chunk */
334 mmioAscend(hmmio, &mmckInfo, 0);
336 mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
337 if (mmioDescend(hmmio, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK))
338 goto errCleanUp;
340 TRACE("Chunk Found ckid=%.4s fccType=%.4s cksize=%08lX\n",
341 (LPSTR)&mmckInfo.ckid, (LPSTR)&mmckInfo.fccType, mmckInfo.cksize);
343 s.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
345 if (waveOutOpen(&hWave, WAVE_MAPPER, lpWaveFormat, (DWORD)PlaySound_Callback,
346 (DWORD)&s, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
347 goto errCleanUp;
349 /* make it so that 3 buffers per second are needed */
350 bufsize = (((lpWaveFormat->nAvgBytesPerSec / 3) - 1) / lpWaveFormat->nBlockAlign + 1) *
351 lpWaveFormat->nBlockAlign;
352 waveHdr = HeapAlloc(GetProcessHeap(), 0, 2 * sizeof(WAVEHDR) + 2 * bufsize);
353 waveHdr[0].lpData = (char*)waveHdr + 2 * sizeof(WAVEHDR);
354 waveHdr[1].lpData = (char*)waveHdr + 2 * sizeof(WAVEHDR) + bufsize;
355 waveHdr[0].dwUser = waveHdr[1].dwUser = 0L;
356 waveHdr[0].dwLoops = waveHdr[1].dwLoops = 0L;
357 waveHdr[0].dwFlags = waveHdr[1].dwFlags = 0L;
358 waveHdr[0].dwBufferLength = waveHdr[1].dwBufferLength = bufsize;
359 if (waveOutPrepareHeader(hWave, &waveHdr[0], sizeof(WAVEHDR)) ||
360 waveOutPrepareHeader(hWave, &waveHdr[1], sizeof(WAVEHDR))) {
361 goto errCleanUp;
364 s.dwEventCount = 1L; /* for first buffer */
366 do {
367 index = 0;
368 left = mmckInfo.cksize;
370 mmioSeek(hmmio, mmckInfo.dwDataOffset, SEEK_SET);
371 while (left)
373 if (WaitForSingleObject(WINMM_IData->psStopEvent, 0) == WAIT_OBJECT_0)
375 wps->bLoop = FALSE;
376 break;
378 count = mmioRead(hmmio, waveHdr[index].lpData, min(bufsize, left));
379 if (count < 1) break;
380 left -= count;
381 waveHdr[index].dwBufferLength = count;
382 waveHdr[index].dwFlags &= ~WHDR_DONE;
383 if (waveOutWrite(hWave, &waveHdr[index], sizeof(WAVEHDR)) == MMSYSERR_NOERROR) {
384 index ^= 1;
385 PlaySound_WaitDone(&s);
387 else FIXME("Couldn't play header\n");
389 bRet = TRUE;
390 } while (wps->bLoop);
392 PlaySound_WaitDone(&s); /* for last buffer */
393 waveOutReset(hWave);
395 waveOutUnprepareHeader(hWave, &waveHdr[0], sizeof(WAVEHDR));
396 waveOutUnprepareHeader(hWave, &waveHdr[1], sizeof(WAVEHDR));
398 errCleanUp:
399 TRACE("Done playing='%s' => %s!\n", debugstr_w(wps->pszSound), bRet ? "ok" : "ko");
400 CloseHandle(s.hEvent);
401 if (waveHdr) HeapFree(GetProcessHeap(), 0, waveHdr);
402 if (lpWaveFormat) HeapFree(GetProcessHeap(), 0, lpWaveFormat);
403 if (hWave) while (waveOutClose(hWave) == WAVERR_STILLPLAYING) Sleep(100);
404 if (hmmio) mmioClose(hmmio, 0);
406 PlaySound_Free(wps);
408 return bRet;
411 BOOL MULTIMEDIA_PlaySound(const void* pszSound, HMODULE hmod, DWORD fdwSound, BOOL bUnicode)
413 WINE_PLAYSOUND* wps = NULL;
415 TRACE("pszSound='%p' hmod=%p fdwSound=%08lX\n",
416 pszSound, hmod, fdwSound);
418 /* FIXME? I see no difference between SND_NOWAIT and SND_NOSTOP !
419 * there could be one if several sounds can be played at once...
421 if ((fdwSound & (SND_NOWAIT | SND_NOSTOP)) && WINMM_IData->lpPlaySound != NULL)
422 return FALSE;
424 /* alloc internal structure, if we need to play something */
425 if (pszSound && !(fdwSound & SND_PURGE))
427 if (!(wps = PlaySound_Alloc(pszSound, hmod, fdwSound, bUnicode)))
428 return FALSE;
431 EnterCriticalSection(&WINMM_IData->cs);
432 /* since several threads can enter PlaySound in parallel, we're not
433 * sure, at this point, that another thread didn't start a new playsound
435 while (WINMM_IData->lpPlaySound != NULL)
437 ResetEvent(WINMM_IData->psLastEvent);
438 /* FIXME: doc says we have to stop all instances of pszSound if it's non
439 * NULL... as of today, we stop all playing instances */
440 SetEvent(WINMM_IData->psStopEvent);
442 LeaveCriticalSection(&WINMM_IData->cs);
443 WaitForSingleObject(WINMM_IData->psLastEvent, INFINITE);
444 EnterCriticalSection(&WINMM_IData->cs);
446 ResetEvent(WINMM_IData->psStopEvent);
449 if (wps) wps->lpNext = WINMM_IData->lpPlaySound;
450 WINMM_IData->lpPlaySound = wps;
451 LeaveCriticalSection(&WINMM_IData->cs);
453 if (!pszSound || (fdwSound & SND_PURGE)) return TRUE;
455 if (fdwSound & SND_ASYNC)
457 DWORD id;
458 wps->bLoop = (fdwSound & SND_LOOP) ? TRUE : FALSE;
459 if (CreateThread(NULL, 0, proc_PlaySound, wps, 0, &id) != 0)
460 return TRUE;
462 else return proc_PlaySound(wps);
464 /* error cases */
465 PlaySound_Free(wps);
466 return FALSE;
469 /**************************************************************************
470 * PlaySoundA [WINMM.@]
472 BOOL WINAPI PlaySoundA(LPCSTR pszSoundA, HMODULE hmod, DWORD fdwSound)
474 return MULTIMEDIA_PlaySound(pszSoundA, hmod, fdwSound, FALSE);
477 /**************************************************************************
478 * PlaySoundW [WINMM.@]
480 BOOL WINAPI PlaySoundW(LPCWSTR pszSoundW, HMODULE hmod, DWORD fdwSound)
482 return MULTIMEDIA_PlaySound(pszSoundW, hmod, fdwSound, TRUE);
485 /**************************************************************************
486 * sndPlaySoundA [WINMM.@]
488 BOOL WINAPI sndPlaySoundA(LPCSTR pszSoundA, UINT uFlags)
490 uFlags &= SND_ASYNC|SND_LOOP|SND_MEMORY|SND_NODEFAULT|SND_NOSTOP|SND_SYNC;
491 return MULTIMEDIA_PlaySound(pszSoundA, 0, uFlags, FALSE);
494 /**************************************************************************
495 * sndPlaySoundW [WINMM.@]
497 BOOL WINAPI sndPlaySoundW(LPCWSTR pszSound, UINT uFlags)
499 uFlags &= SND_ASYNC|SND_LOOP|SND_MEMORY|SND_NODEFAULT|SND_NOSTOP|SND_SYNC;
500 return MULTIMEDIA_PlaySound(pszSound, 0, uFlags, TRUE);
503 /**************************************************************************
504 * mmsystemGetVersion [WINMM.@]
506 UINT WINAPI mmsystemGetVersion(void)
508 TRACE("3.10 (Win95?)\n");
509 return 0x030a;