include: Mark imported functions with hidden visibility.
[wine/multimedia.git] / dlls / winmm / playsound.c
blob96d3249a9aaa25fdb74c2062b24157ec467d9ea3
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <stdarg.h>
25 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "mmsystem.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winreg.h"
33 #include "winemm.h"
34 #include "winternl.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
40 typedef struct tagWINE_PLAYSOUND
42 unsigned bLoop : 1,
43 bAlloc : 1;
44 LPCWSTR pszSound;
45 HMODULE hMod;
46 DWORD fdwSound;
47 HANDLE hThread;
48 HWAVEOUT hWave;
49 struct tagWINE_PLAYSOUND* lpNext;
50 } WINE_PLAYSOUND;
52 static WINE_PLAYSOUND *PlaySoundList;
54 static HMMIO get_mmioFromFile(LPCWSTR lpszName)
56 HMMIO ret;
57 WCHAR buf[256];
58 LPWSTR dummy;
60 ret = mmioOpenW((LPWSTR)lpszName, NULL,
61 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
62 if (ret != 0) return ret;
63 if (SearchPathW(NULL, lpszName, NULL, sizeof(buf)/sizeof(buf[0]), buf, &dummy))
65 return mmioOpenW(buf, NULL,
66 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
68 return 0;
71 static HMMIO get_mmioFromProfile(UINT uFlags, LPCWSTR lpszName)
73 WCHAR str[128];
74 LPWSTR ptr;
75 HMMIO hmmio;
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','\\',
83 'A','p','p','s',0};
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++);
97 if (*ptr) *ptr = 0;
98 hmmio = mmioOpenW(str, NULL, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
99 if (hmmio != 0) return hmmio;
100 next:
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)
108 DWORD len;
110 err = 1; /* error */
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;
117 if (*ptr == '\\')
119 err = RegOpenKeyW(hRegSnd, ptr+1, &hRegApp);
120 break;
125 else
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);
136 if (err != 0)
138 err = RegOpenKeyW(hScheme, wszDotCurrent, &hSnd);
139 RegCloseKey(hScheme);
140 if (err != 0)
141 goto none;
143 count = sizeof(str)/sizeof(str[0]);
144 err = RegQueryValueExW(hSnd, NULL, 0, &type, (LPBYTE)str, &count);
145 RegCloseKey(hSnd);
146 if (err != 0 || !*str) goto none;
147 hmmio = mmioOpenW(str, NULL, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
148 if (hmmio) return hmmio;
149 none:
150 WARN("can't find SystemSound=%s !\n", debugstr_w(lpszName));
151 return 0;
154 struct playsound_data
156 HANDLE hEvent;
157 LONG dwEventCount;
160 static void CALLBACK PlaySound_Callback(HWAVEOUT hwo, UINT uMsg,
161 DWORD dwInstance,
162 DWORD dwParam1, DWORD dwParam2)
164 struct playsound_data* s = (struct playsound_data*)dwInstance;
166 switch (uMsg) {
167 case WOM_OPEN:
168 case WOM_CLOSE:
169 break;
170 case WOM_DONE:
171 InterlockedIncrement(&s->dwEventCount);
172 TRACE("Returning waveHdr=%x\n", dwParam1);
173 SetEvent(s->hEvent);
174 break;
175 default:
176 ERR("Unknown uMsg=%d\n", uMsg);
180 static void PlaySound_WaitDone(struct playsound_data* s)
182 for (;;) {
183 ResetEvent(s->hEvent);
184 if (InterlockedDecrement(&s->dwEventCount) >= 0) break;
185 InterlockedIncrement(&s->dwEventCount);
187 WaitForSingleObject(s->hEvent, INFINITE);
191 static BOOL PlaySound_IsString(DWORD fdwSound, const void* psz)
193 /* SND_RESOURCE is 0x40004 while
194 * SND_MEMORY is 0x00004
196 switch (fdwSound & (SND_RESOURCE|SND_ALIAS|SND_FILENAME))
198 case SND_RESOURCE: return HIWORD(psz) != 0; /* by name or by ID ? */
199 case SND_MEMORY: return FALSE;
200 case SND_ALIAS: /* what about ALIAS_ID ??? */
201 case SND_FILENAME:
202 case 0: return TRUE;
203 default: FIXME("WTF\n"); return FALSE;
207 static void PlaySound_Free(WINE_PLAYSOUND* wps)
209 WINE_PLAYSOUND** p;
211 EnterCriticalSection(&WINMM_cs);
212 for (p = &PlaySoundList; *p && *p != wps; p = &((*p)->lpNext));
213 if (*p) *p = (*p)->lpNext;
214 if (PlaySoundList == NULL) SetEvent(psLastEvent);
215 LeaveCriticalSection(&WINMM_cs);
216 if (wps->bAlloc) HeapFree(GetProcessHeap(), 0, (void*)wps->pszSound);
217 if (wps->hThread) CloseHandle(wps->hThread);
218 HeapFree(GetProcessHeap(), 0, wps);
221 static WINE_PLAYSOUND* PlaySound_Alloc(const void* pszSound, HMODULE hmod,
222 DWORD fdwSound, BOOL bUnicode)
224 WINE_PLAYSOUND* wps;
226 wps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wps));
227 if (!wps) return NULL;
229 wps->hMod = hmod;
230 wps->fdwSound = fdwSound;
231 if (PlaySound_IsString(fdwSound, pszSound))
233 if (bUnicode)
235 if (fdwSound & SND_ASYNC)
237 LPWSTR sound = HeapAlloc(GetProcessHeap(), 0,
238 (lstrlenW(pszSound)+1) * sizeof(WCHAR));
239 if (!sound) goto oom_error;
240 wps->pszSound = lstrcpyW(sound, pszSound);
241 wps->bAlloc = TRUE;
243 else
244 wps->pszSound = pszSound;
246 else
248 UNICODE_STRING usBuffer;
249 RtlCreateUnicodeStringFromAsciiz(&usBuffer, pszSound);
250 wps->pszSound = usBuffer.Buffer;
251 if (!wps->pszSound) goto oom_error;
252 wps->bAlloc = TRUE;
255 else
256 wps->pszSound = pszSound;
258 return wps;
259 oom_error:
260 PlaySound_Free(wps);
261 return NULL;
264 static DWORD WINAPI proc_PlaySound(LPVOID arg)
266 WINE_PLAYSOUND* wps = (WINE_PLAYSOUND*)arg;
267 BOOL bRet = FALSE;
268 HMMIO hmmio = 0;
269 MMCKINFO ckMainRIFF;
270 MMCKINFO mmckInfo;
271 LPWAVEFORMATEX lpWaveFormat = NULL;
272 LPWAVEHDR waveHdr = NULL;
273 INT count, bufsize, left, index;
274 struct playsound_data s;
275 void* data;
277 s.hEvent = 0;
279 TRACE("SoundName=%s !\n", debugstr_w(wps->pszSound));
281 /* if resource, grab it */
282 if ((wps->fdwSound & SND_RESOURCE) == SND_RESOURCE) {
283 static const WCHAR wszWave[] = {'W','A','V','E',0};
284 HRSRC hRes;
285 HGLOBAL hGlob;
287 if ((hRes = FindResourceW(wps->hMod, wps->pszSound, wszWave)) == 0 ||
288 (hGlob = LoadResource(wps->hMod, hRes)) == 0)
289 goto errCleanUp;
290 if ((data = LockResource(hGlob)) == NULL) {
291 FreeResource(hGlob);
292 goto errCleanUp;
294 FreeResource(hGlob);
295 } else
296 data = (void*)wps->pszSound;
298 /* construct an MMIO stream (either in memory, or from a file */
299 if (wps->fdwSound & SND_MEMORY)
300 { /* NOTE: SND_RESOURCE has the SND_MEMORY bit set */
301 MMIOINFO mminfo;
303 memset(&mminfo, 0, sizeof(mminfo));
304 mminfo.fccIOProc = FOURCC_MEM;
305 mminfo.pchBuffer = (LPSTR)data;
306 mminfo.cchBuffer = -1; /* FIXME: when a resource, could grab real size */
307 TRACE("Memory sound %p\n", data);
308 hmmio = mmioOpenW(NULL, &mminfo, MMIO_READ);
310 else if (wps->fdwSound & SND_ALIAS)
312 hmmio = get_mmioFromProfile(wps->fdwSound, wps->pszSound);
314 else if (wps->fdwSound & SND_FILENAME)
316 hmmio = get_mmioFromFile(wps->pszSound);
318 else
320 if ((hmmio = get_mmioFromProfile(wps->fdwSound | SND_NODEFAULT, wps->pszSound)) == 0)
322 if ((hmmio = get_mmioFromFile(wps->pszSound)) == 0)
324 hmmio = get_mmioFromProfile(wps->fdwSound, wps->pszSound);
328 if (hmmio == 0) goto errCleanUp;
330 if (mmioDescend(hmmio, &ckMainRIFF, NULL, 0))
331 goto errCleanUp;
333 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
334 (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, ckMainRIFF.cksize);
336 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
337 (ckMainRIFF.fccType != mmioFOURCC('W', 'A', 'V', 'E')))
338 goto errCleanUp;
340 mmckInfo.ckid = mmioFOURCC('f', 'm', 't', ' ');
341 if (mmioDescend(hmmio, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK))
342 goto errCleanUp;
344 TRACE("Chunk Found ckid=%.4s fccType=%08x cksize=%08X\n",
345 (LPSTR)&mmckInfo.ckid, mmckInfo.fccType, mmckInfo.cksize);
347 lpWaveFormat = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
348 if (mmioRead(hmmio, (HPSTR)lpWaveFormat, mmckInfo.cksize) < sizeof(WAVEFORMAT))
349 goto errCleanUp;
351 TRACE("wFormatTag=%04X !\n", lpWaveFormat->wFormatTag);
352 TRACE("nChannels=%d\n", lpWaveFormat->nChannels);
353 TRACE("nSamplesPerSec=%d\n", lpWaveFormat->nSamplesPerSec);
354 TRACE("nAvgBytesPerSec=%d\n", lpWaveFormat->nAvgBytesPerSec);
355 TRACE("nBlockAlign=%d\n", lpWaveFormat->nBlockAlign);
356 TRACE("wBitsPerSample=%u !\n", lpWaveFormat->wBitsPerSample);
358 /* move to end of 'fmt ' chunk */
359 mmioAscend(hmmio, &mmckInfo, 0);
361 mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
362 if (mmioDescend(hmmio, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK))
363 goto errCleanUp;
365 TRACE("Chunk Found ckid=%.4s fccType=%08x cksize=%08X\n",
366 (LPSTR)&mmckInfo.ckid, mmckInfo.fccType, mmckInfo.cksize);
368 s.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
370 if (waveOutOpen(&wps->hWave, WAVE_MAPPER, lpWaveFormat, (DWORD)PlaySound_Callback,
371 (DWORD)&s, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
372 goto errCleanUp;
374 /* make it so that 3 buffers per second are needed */
375 bufsize = (((lpWaveFormat->nAvgBytesPerSec / 3) - 1) / lpWaveFormat->nBlockAlign + 1) *
376 lpWaveFormat->nBlockAlign;
377 waveHdr = HeapAlloc(GetProcessHeap(), 0, 2 * sizeof(WAVEHDR) + 2 * bufsize);
378 waveHdr[0].lpData = (char*)waveHdr + 2 * sizeof(WAVEHDR);
379 waveHdr[1].lpData = (char*)waveHdr + 2 * sizeof(WAVEHDR) + bufsize;
380 waveHdr[0].dwUser = waveHdr[1].dwUser = 0L;
381 waveHdr[0].dwLoops = waveHdr[1].dwLoops = 0L;
382 waveHdr[0].dwFlags = waveHdr[1].dwFlags = 0L;
383 waveHdr[0].dwBufferLength = waveHdr[1].dwBufferLength = bufsize;
384 if (waveOutPrepareHeader(wps->hWave, &waveHdr[0], sizeof(WAVEHDR)) ||
385 waveOutPrepareHeader(wps->hWave, &waveHdr[1], sizeof(WAVEHDR))) {
386 goto errCleanUp;
389 s.dwEventCount = 1L; /* for first buffer */
390 index = 0;
392 do {
393 left = mmckInfo.cksize;
395 mmioSeek(hmmio, mmckInfo.dwDataOffset, SEEK_SET);
396 while (left)
398 if (WaitForSingleObject(psStopEvent, 0) == WAIT_OBJECT_0)
400 wps->bLoop = FALSE;
401 break;
403 count = mmioRead(hmmio, waveHdr[index].lpData, min(bufsize, left));
404 if (count < 1) break;
405 left -= count;
406 waveHdr[index].dwBufferLength = count;
407 waveHdr[index].dwFlags &= ~WHDR_DONE;
408 if (waveOutWrite(wps->hWave, &waveHdr[index], sizeof(WAVEHDR)) == MMSYSERR_NOERROR) {
409 index ^= 1;
410 PlaySound_WaitDone(&s);
412 else FIXME("Couldn't play header\n");
414 bRet = TRUE;
415 } while (wps->bLoop);
417 PlaySound_WaitDone(&s); /* for last buffer */
418 waveOutReset(wps->hWave);
420 waveOutUnprepareHeader(wps->hWave, &waveHdr[0], sizeof(WAVEHDR));
421 waveOutUnprepareHeader(wps->hWave, &waveHdr[1], sizeof(WAVEHDR));
423 errCleanUp:
424 TRACE("Done playing=%s => %s!\n", debugstr_w(wps->pszSound), bRet ? "ok" : "ko");
425 CloseHandle(s.hEvent);
426 HeapFree(GetProcessHeap(), 0, waveHdr);
427 HeapFree(GetProcessHeap(), 0, lpWaveFormat);
428 if (wps->hWave) while (waveOutClose(wps->hWave) == WAVERR_STILLPLAYING) Sleep(100);
429 if (hmmio) mmioClose(hmmio, 0);
431 PlaySound_Free(wps);
433 return bRet;
436 static BOOL MULTIMEDIA_PlaySound(const void* pszSound, HMODULE hmod, DWORD fdwSound, BOOL bUnicode)
438 WINE_PLAYSOUND* wps = NULL;
440 TRACE("pszSound='%p' hmod=%p fdwSound=%08X\n",
441 pszSound, hmod, fdwSound);
443 /* FIXME? I see no difference between SND_NOWAIT and SND_NOSTOP !
444 * there could be one if several sounds can be played at once...
446 if ((fdwSound & (SND_NOWAIT | SND_NOSTOP)) && PlaySoundList != NULL)
447 return FALSE;
449 /* alloc internal structure, if we need to play something */
450 if (pszSound && !(fdwSound & SND_PURGE))
452 if (!(wps = PlaySound_Alloc(pszSound, hmod, fdwSound, bUnicode)))
453 return FALSE;
456 EnterCriticalSection(&WINMM_cs);
457 /* since several threads can enter PlaySound in parallel, we're not
458 * sure, at this point, that another thread didn't start a new playsound
460 while (PlaySoundList != NULL)
462 ResetEvent(psLastEvent);
463 /* FIXME: doc says we have to stop all instances of pszSound if it's non
464 * NULL... as of today, we stop all playing instances */
465 SetEvent(psStopEvent);
467 waveOutReset(PlaySoundList->hWave);
468 LeaveCriticalSection(&WINMM_cs);
469 WaitForSingleObject(psLastEvent, INFINITE);
470 EnterCriticalSection(&WINMM_cs);
472 ResetEvent(psStopEvent);
475 if (wps) wps->lpNext = PlaySoundList;
476 PlaySoundList = wps;
477 LeaveCriticalSection(&WINMM_cs);
479 if (!pszSound || (fdwSound & SND_PURGE)) return TRUE;
481 if (fdwSound & SND_ASYNC)
483 DWORD id;
484 HANDLE handle;
485 wps->bLoop = (fdwSound & SND_LOOP) ? TRUE : FALSE;
486 if ((handle = CreateThread(NULL, 0, proc_PlaySound, wps, 0, &id)) != 0) {
487 wps->hThread = handle;
488 SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL);
489 return TRUE;
492 else return proc_PlaySound(wps);
494 /* error cases */
495 PlaySound_Free(wps);
496 return FALSE;
499 /**************************************************************************
500 * PlaySoundA [WINMM.@]
502 BOOL WINAPI PlaySoundA(LPCSTR pszSoundA, HMODULE hmod, DWORD fdwSound)
504 return MULTIMEDIA_PlaySound(pszSoundA, hmod, fdwSound, FALSE);
507 /**************************************************************************
508 * PlaySoundW [WINMM.@]
510 BOOL WINAPI PlaySoundW(LPCWSTR pszSoundW, HMODULE hmod, DWORD fdwSound)
512 return MULTIMEDIA_PlaySound(pszSoundW, hmod, fdwSound, TRUE);
515 /**************************************************************************
516 * sndPlaySoundA [WINMM.@]
518 BOOL WINAPI sndPlaySoundA(LPCSTR pszSoundA, UINT uFlags)
520 uFlags &= SND_ASYNC|SND_LOOP|SND_MEMORY|SND_NODEFAULT|SND_NOSTOP|SND_SYNC;
521 return MULTIMEDIA_PlaySound(pszSoundA, 0, uFlags, FALSE);
524 /**************************************************************************
525 * sndPlaySoundW [WINMM.@]
527 BOOL WINAPI sndPlaySoundW(LPCWSTR pszSound, UINT uFlags)
529 uFlags &= SND_ASYNC|SND_LOOP|SND_MEMORY|SND_NODEFAULT|SND_NOSTOP|SND_SYNC;
530 return MULTIMEDIA_PlaySound(pszSound, 0, uFlags, TRUE);
533 /**************************************************************************
534 * mmsystemGetVersion [WINMM.@]
536 UINT WINAPI mmsystemGetVersion(void)
538 TRACE("3.10 (Win95?)\n");
539 return 0x030a;