wow64: In wow64_NtSetInformationToken forward TokenIntegrityLevel.
[wine.git] / dlls / winmm / playsound.c
blobbd26bf481f3288a0834bb04bcafe60abbd0caee6
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * MMSYSTEM 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 HWAVEOUT hWave;
48 } WINE_PLAYSOUND;
50 static WINE_PLAYSOUND *PlaySoundCurrent;
51 static BOOL bPlaySoundStop;
53 static HMMIO get_mmioFromFile(LPCWSTR lpszName)
55 HMMIO ret;
56 WCHAR buf[256];
57 LPWSTR dummy;
59 ret = mmioOpenW((LPWSTR)lpszName, NULL,
60 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
61 if (ret != 0) return ret;
62 if (SearchPathW(NULL, lpszName, L".wav", ARRAY_SIZE(buf), buf, &dummy))
64 return mmioOpenW(buf, NULL,
65 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
67 return 0;
70 static HMMIO get_mmioFromProfile(UINT uFlags, LPCWSTR lpszName)
72 WCHAR str[128];
73 LPWSTR ptr;
74 HMMIO hmmio;
75 HKEY hRegSnd, hRegApp, hScheme, hSnd;
76 DWORD err, type, count;
78 TRACE("searching in SystemSound list for %s\n", debugstr_w(lpszName));
79 GetProfileStringW(L"Sounds", lpszName, L"", str, ARRAY_SIZE(str));
80 if (!*str)
82 if (uFlags & SND_NODEFAULT) goto next;
83 GetProfileStringW(L"Sounds", L"Default", L"", str, ARRAY_SIZE(str));
84 if (!*str) goto next;
86 for (ptr = str; *ptr && *ptr != ','; ptr++);
87 if (*ptr) *ptr = 0;
88 hmmio = mmioOpenW(str, NULL, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
89 if (hmmio != 0) return hmmio;
90 next:
91 /* we look up the registry under
92 * HKCU\AppEvents\Schemes\Apps\.Default
93 * HKCU\AppEvents\Schemes\Apps\<AppName>
95 if (RegOpenKeyW(HKEY_CURRENT_USER, L"AppEvents\\Schemes\\Apps", &hRegSnd) != 0) goto none;
96 if (uFlags & SND_APPLICATION)
98 DWORD len;
100 err = 1; /* error */
101 len = GetModuleFileNameW(0, str, ARRAY_SIZE(str));
102 if (len > 0 && len < ARRAY_SIZE(str))
104 for (ptr = str + lstrlenW(str) - 1; ptr >= str; ptr--)
106 if (*ptr == '.') *ptr = 0;
107 if (*ptr == '\\')
109 err = RegOpenKeyW(hRegSnd, ptr+1, &hRegApp);
110 break;
115 else
117 err = RegOpenKeyW(hRegSnd, L".Default", &hRegApp);
119 RegCloseKey(hRegSnd);
120 if (err != 0) goto none;
121 err = RegOpenKeyW(hRegApp, lpszName, &hScheme);
122 RegCloseKey(hRegApp);
123 if (err != 0) goto none;
124 /* what's the difference between .Current and .Default ? */
125 err = RegOpenKeyW(hScheme, L".Default", &hSnd);
126 if (err != 0)
128 err = RegOpenKeyW(hScheme, L".Current", &hSnd);
129 RegCloseKey(hScheme);
130 if (err != 0)
131 goto none;
133 count = sizeof(str);
134 err = RegQueryValueExW(hSnd, NULL, 0, &type, (LPBYTE)str, &count);
135 RegCloseKey(hSnd);
136 if (err != 0 || !*str) goto none;
137 hmmio = mmioOpenW(str, NULL, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
138 if (hmmio) return hmmio;
139 none:
140 WARN("can't find SystemSound=%s !\n", debugstr_w(lpszName));
141 return 0;
144 struct playsound_data
146 HANDLE hEvent;
147 LONG dwEventCount;
150 static void CALLBACK PlaySound_Callback(HWAVEOUT hwo, UINT uMsg,
151 DWORD_PTR dwInstance,
152 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
154 struct playsound_data* s = (struct playsound_data*)dwInstance;
156 switch (uMsg) {
157 case WOM_OPEN:
158 case WOM_CLOSE:
159 break;
160 case WOM_DONE:
161 InterlockedIncrement(&s->dwEventCount);
162 TRACE("Returning waveHdr=%Ix\n", dwParam1);
163 SetEvent(s->hEvent);
164 break;
165 default:
166 ERR("Unknown uMsg=%d\n", uMsg);
170 static void PlaySound_WaitDone(struct playsound_data* s)
172 for (;;) {
173 if (InterlockedDecrement(&s->dwEventCount) >= 0) break;
174 InterlockedIncrement(&s->dwEventCount);
176 WaitForSingleObject(s->hEvent, INFINITE);
180 static BOOL PlaySound_IsString(DWORD fdwSound, const void* psz)
182 /* SND_RESOURCE is 0x40004 while
183 * SND_MEMORY is 0x00004
185 switch (fdwSound & (SND_RESOURCE|SND_ALIAS_ID|SND_FILENAME))
187 case SND_RESOURCE: return HIWORD(psz) != 0; /* by name or by ID ? */
188 case SND_ALIAS_ID:
189 case SND_MEMORY: return FALSE;
190 case SND_ALIAS:
191 case SND_FILENAME:
192 case SND_ALIAS|SND_FILENAME:
193 case 0: return TRUE;
194 default: FIXME("WTF\n"); return FALSE;
198 static void PlaySound_Free(WINE_PLAYSOUND* wps)
200 EnterCriticalSection(&WINMM_cs);
201 PlaySoundCurrent = NULL;
202 SetEvent(psLastEvent);
203 LeaveCriticalSection(&WINMM_cs);
204 if (wps->bAlloc) free((void*)wps->pszSound);
205 free(wps);
208 static WINE_PLAYSOUND* PlaySound_Alloc(const void* pszSound, HMODULE hmod,
209 DWORD fdwSound, BOOL bUnicode)
211 WINE_PLAYSOUND* wps;
213 wps = calloc(1, sizeof(*wps));
214 if (!wps) return NULL;
216 wps->hMod = hmod;
217 wps->fdwSound = fdwSound;
218 if (PlaySound_IsString(fdwSound, pszSound))
220 if (bUnicode)
222 if (fdwSound & SND_ASYNC)
224 wps->pszSound = wcsdup(pszSound);
225 if (!wps->pszSound) goto oom_error;
226 wps->bAlloc = TRUE;
228 else
229 wps->pszSound = pszSound;
231 else
233 UNICODE_STRING usBuffer;
234 if (!RtlCreateUnicodeStringFromAsciiz(&usBuffer, pszSound)) goto oom_error;
235 wps->pszSound = wcsdup(usBuffer.Buffer);
236 RtlFreeUnicodeString(&usBuffer);
237 if (!wps->pszSound) goto oom_error;
238 wps->bAlloc = TRUE;
241 else
242 wps->pszSound = pszSound;
244 return wps;
245 oom_error:
246 if (wps->bAlloc) free((void*)wps->pszSound);
247 free(wps);
248 return NULL;
251 static DWORD WINAPI proc_PlaySound(LPVOID arg)
253 WINE_PLAYSOUND* wps = arg;
254 BOOL bRet = FALSE;
255 HMMIO hmmio = 0;
256 MMCKINFO ckMainRIFF;
257 MMCKINFO mmckInfo;
258 LPWAVEFORMATEX lpWaveFormat = NULL;
259 HWAVEOUT hWave = 0;
260 LPWAVEHDR waveHdr = NULL;
261 INT count, bufsize, left, index;
262 struct playsound_data s;
263 void* data;
264 LONG r;
266 s.hEvent = 0;
268 TRACE("SoundName=%s !\n", debugstr_w(wps->pszSound));
270 /* if resource, grab it */
271 if ((wps->fdwSound & SND_RESOURCE) == SND_RESOURCE) {
272 HRSRC hRes;
273 HGLOBAL hGlob;
275 if ((hRes = FindResourceW(wps->hMod, wps->pszSound, L"WAVE")) == 0 ||
276 (hGlob = LoadResource(wps->hMod, hRes)) == 0)
277 goto errCleanUp;
278 if ((data = LockResource(hGlob)) == NULL) {
279 FreeResource(hGlob);
280 goto errCleanUp;
282 FreeResource(hGlob);
283 } else
284 data = (void*)wps->pszSound;
286 /* construct an MMIO stream (either in memory, or from a file */
287 if (wps->fdwSound & SND_MEMORY)
288 { /* NOTE: SND_RESOURCE has the SND_MEMORY bit set */
289 MMIOINFO mminfo;
291 memset(&mminfo, 0, sizeof(mminfo));
292 mminfo.fccIOProc = FOURCC_MEM;
293 mminfo.pchBuffer = data;
294 mminfo.cchBuffer = -1; /* FIXME: when a resource, could grab real size */
295 TRACE("Memory sound %p\n", data);
296 hmmio = mmioOpenW(NULL, &mminfo, MMIO_READ);
298 if (!hmmio && wps->fdwSound & SND_ALIAS)
300 if ((wps->fdwSound & SND_ALIAS_ID) == SND_ALIAS_ID)
302 wps->fdwSound &= ~(SND_ALIAS_ID ^ SND_ALIAS);
303 if (wps->pszSound == (LPCWSTR)SND_ALIAS_SYSTEMASTERISK)
304 wps->pszSound = L"SystemAsterisk";
305 else if (wps->pszSound == (LPCWSTR)SND_ALIAS_SYSTEMDEFAULT)
306 wps->pszSound = L"SystemDefault";
307 else if (wps->pszSound == (LPCWSTR)SND_ALIAS_SYSTEMEXCLAMATION)
308 wps->pszSound = L"SystemExclamation";
309 else if (wps->pszSound == (LPCWSTR)SND_ALIAS_SYSTEMEXIT)
310 wps->pszSound = L"SystemExit";
311 else if (wps->pszSound == (LPCWSTR)SND_ALIAS_SYSTEMHAND)
312 wps->pszSound = L"SystemHand";
313 else if (wps->pszSound == (LPCWSTR)SND_ALIAS_SYSTEMQUESTION)
314 wps->pszSound = L"SystemQuestion";
315 else if (wps->pszSound == (LPCWSTR)SND_ALIAS_SYSTEMSTART)
316 wps->pszSound = L"SystemStart";
317 else if (wps->pszSound == (LPCWSTR)SND_ALIAS_SYSTEMWELCOME)
318 wps->pszSound = L"SystemWelcome";
319 else goto errCleanUp;
321 hmmio = get_mmioFromProfile(wps->fdwSound, wps->pszSound);
322 if (!hmmio)
324 wps->fdwSound &= ~SND_ALIAS;
325 wps->fdwSound |= SND_FILENAME;
328 if (!hmmio && wps->fdwSound & SND_FILENAME)
330 hmmio = get_mmioFromFile(wps->pszSound);
332 if (!(wps->fdwSound & (SND_FILENAME|SND_ALIAS|SND_MEMORY)))
334 if ((hmmio = get_mmioFromProfile(wps->fdwSound | SND_NODEFAULT, wps->pszSound)) == 0)
336 if ((hmmio = get_mmioFromFile(wps->pszSound)) == 0)
338 hmmio = get_mmioFromProfile(wps->fdwSound, wps->pszSound);
342 if (hmmio == 0) goto errCleanUp;
344 if (mmioDescend(hmmio, &ckMainRIFF, NULL, 0))
345 goto errCleanUp;
347 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08lX\n",
348 (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, ckMainRIFF.cksize);
350 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
351 (ckMainRIFF.fccType != mmioFOURCC('W', 'A', 'V', 'E')))
352 goto errCleanUp;
354 mmckInfo.ckid = mmioFOURCC('f', 'm', 't', ' ');
355 if (mmioDescend(hmmio, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK))
356 goto errCleanUp;
358 TRACE("Chunk Found ckid=%.4s fccType=%08lx cksize=%08lX\n",
359 (LPSTR)&mmckInfo.ckid, mmckInfo.fccType, mmckInfo.cksize);
361 lpWaveFormat = malloc(mmckInfo.cksize);
362 if (!lpWaveFormat)
363 goto errCleanUp;
364 r = mmioRead(hmmio, (HPSTR)lpWaveFormat, mmckInfo.cksize);
365 if (r < 0 || r < sizeof(PCMWAVEFORMAT))
366 goto errCleanUp;
368 TRACE("wFormatTag=%04X !\n", lpWaveFormat->wFormatTag);
369 TRACE("nChannels=%d\n", lpWaveFormat->nChannels);
370 TRACE("nSamplesPerSec=%ld\n", lpWaveFormat->nSamplesPerSec);
371 TRACE("nAvgBytesPerSec=%ld\n", lpWaveFormat->nAvgBytesPerSec);
372 TRACE("nBlockAlign=%d\n", lpWaveFormat->nBlockAlign);
373 TRACE("wBitsPerSample=%u !\n", lpWaveFormat->wBitsPerSample);
375 /* move to end of 'fmt ' chunk */
376 mmioAscend(hmmio, &mmckInfo, 0);
378 mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
379 if (mmioDescend(hmmio, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK))
380 goto errCleanUp;
382 TRACE("Chunk Found ckid=%.4s fccType=%08lx cksize=%08lX\n",
383 (LPSTR)&mmckInfo.ckid, mmckInfo.fccType, mmckInfo.cksize);
385 s.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
386 if (!s.hEvent || bPlaySoundStop)
387 goto errCleanUp;
389 if (waveOutOpen(&hWave, WAVE_MAPPER, lpWaveFormat, (DWORD_PTR)PlaySound_Callback,
390 (DWORD_PTR)&s, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
391 goto errCleanUp;
393 /* make it so that 3 buffers per second are needed */
394 bufsize = (((lpWaveFormat->nAvgBytesPerSec / 3) - 1) / lpWaveFormat->nBlockAlign + 1) *
395 lpWaveFormat->nBlockAlign;
396 waveHdr = malloc(2 * sizeof(WAVEHDR) + 2 * bufsize);
397 if (!waveHdr)
398 goto errCleanUp;
399 waveHdr[0].lpData = (char*)waveHdr + 2 * sizeof(WAVEHDR);
400 waveHdr[1].lpData = (char*)waveHdr + 2 * sizeof(WAVEHDR) + bufsize;
401 waveHdr[0].dwUser = waveHdr[1].dwUser = 0L;
402 waveHdr[0].dwLoops = waveHdr[1].dwLoops = 0L;
403 waveHdr[0].dwFlags = waveHdr[1].dwFlags = 0L;
404 waveHdr[0].dwBufferLength = waveHdr[1].dwBufferLength = bufsize;
405 if (waveOutPrepareHeader(hWave, &waveHdr[0], sizeof(WAVEHDR)) ||
406 waveOutPrepareHeader(hWave, &waveHdr[1], sizeof(WAVEHDR))) {
407 goto errCleanUp;
410 wps->hWave = hWave;
411 s.dwEventCount = 1L; /* for first buffer */
412 index = 0;
414 do {
415 left = mmckInfo.cksize;
417 mmioSeek(hmmio, mmckInfo.dwDataOffset, SEEK_SET);
418 while (left)
420 if (bPlaySoundStop)
422 wps->bLoop = FALSE;
423 break;
425 count = mmioRead(hmmio, waveHdr[index].lpData, min(bufsize, left));
426 if (count < 1) break;
427 left -= count;
428 waveHdr[index].dwBufferLength = count;
429 if (waveOutWrite(hWave, &waveHdr[index], sizeof(WAVEHDR)) == MMSYSERR_NOERROR) {
430 index ^= 1;
431 PlaySound_WaitDone(&s);
433 else {
434 ERR("Aborting play loop, waveOutWrite error\n");
435 wps->bLoop = FALSE;
436 break;
439 bRet = TRUE;
440 } while (wps->bLoop);
442 PlaySound_WaitDone(&s); /* to balance first buffer */
443 waveOutReset(hWave);
445 waveOutUnprepareHeader(hWave, &waveHdr[0], sizeof(WAVEHDR));
446 waveOutUnprepareHeader(hWave, &waveHdr[1], sizeof(WAVEHDR));
448 errCleanUp:
449 TRACE("Done playing=%s => %s!\n", debugstr_w(wps->pszSound), bRet ? "ok" : "ko");
450 free(lpWaveFormat);
451 if (hWave)
453 EnterCriticalSection(&WINMM_cs);
454 /* the CS prevents a concurrent waveOutReset */
455 wps->hWave = 0;
456 LeaveCriticalSection(&WINMM_cs);
457 while (waveOutClose(hWave) == WAVERR_STILLPLAYING)
458 Sleep(100);
460 CloseHandle(s.hEvent);
461 free(waveHdr);
462 if (hmmio) mmioClose(hmmio, 0);
464 PlaySound_Free(wps);
466 return bRet;
469 static BOOL MULTIMEDIA_PlaySound(const void* pszSound, HMODULE hmod, DWORD fdwSound, BOOL bUnicode)
471 WINE_PLAYSOUND* wps = NULL;
473 TRACE("pszSound='%p' hmod=%p fdwSound=%08lX\n",
474 pszSound, hmod, fdwSound);
476 /* SND_NOWAIT is ignored in w95/2k/xp. */
477 if ((fdwSound & SND_NOSTOP) && PlaySoundCurrent != NULL)
478 return FALSE;
480 /* alloc internal structure, if we need to play something */
481 if (pszSound && !(fdwSound & SND_PURGE))
483 if (!(wps = PlaySound_Alloc(pszSound, hmod, fdwSound, bUnicode)))
484 return FALSE;
487 EnterCriticalSection(&WINMM_cs);
488 /* since several threads can enter PlaySound in parallel, we're not
489 * sure, at this point, that another thread didn't start a new playsound
491 while (PlaySoundCurrent != NULL)
493 ResetEvent(psLastEvent);
494 /* FIXME: doc says we have to stop all instances of pszSound if it's non
495 * NULL... as of today, we stop all playing instances */
496 bPlaySoundStop = TRUE;
497 if(PlaySoundCurrent->hWave)
498 waveOutReset(PlaySoundCurrent->hWave);
500 LeaveCriticalSection(&WINMM_cs);
501 WaitForSingleObject(psLastEvent, INFINITE);
502 EnterCriticalSection(&WINMM_cs);
504 bPlaySoundStop = FALSE;
507 PlaySoundCurrent = wps;
508 LeaveCriticalSection(&WINMM_cs);
510 if (!wps) return TRUE;
512 if (fdwSound & SND_ASYNC)
514 HANDLE handle;
515 wps->bLoop = (fdwSound & SND_LOOP) != 0;
516 if ((handle = CreateThread(NULL, 0, proc_PlaySound, wps, 0, NULL)) != 0) {
517 SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL);
518 CloseHandle(handle);
519 return TRUE;
522 else return proc_PlaySound(wps);
524 /* error cases */
525 PlaySound_Free(wps);
526 return FALSE;
529 /**************************************************************************
530 * PlaySoundA [WINMM.@]
532 BOOL WINAPI PlaySoundA(LPCSTR pszSoundA, HMODULE hmod, DWORD fdwSound)
534 return MULTIMEDIA_PlaySound(pszSoundA, hmod, fdwSound, FALSE);
537 /**************************************************************************
538 * PlaySoundW [WINMM.@]
540 BOOL WINAPI PlaySoundW(LPCWSTR pszSoundW, HMODULE hmod, DWORD fdwSound)
542 return MULTIMEDIA_PlaySound(pszSoundW, hmod, fdwSound, TRUE);
545 /**************************************************************************
546 * sndPlaySoundA [WINMM.@]
548 BOOL WINAPI sndPlaySoundA(LPCSTR pszSoundA, UINT uFlags)
550 uFlags &= SND_RESOURCE|SND_ALIAS_ID|SND_FILENAME|SND_ASYNC|SND_LOOP|SND_MEMORY|SND_NODEFAULT|SND_NOSTOP|SND_SYNC;
551 return MULTIMEDIA_PlaySound(pszSoundA, 0, uFlags, FALSE);
554 /**************************************************************************
555 * sndPlaySoundW [WINMM.@]
557 BOOL WINAPI sndPlaySoundW(LPCWSTR pszSound, UINT uFlags)
559 uFlags &= SND_RESOURCE|SND_ALIAS_ID|SND_FILENAME|SND_ASYNC|SND_LOOP|SND_MEMORY|SND_NODEFAULT|SND_NOSTOP|SND_SYNC;
560 return MULTIMEDIA_PlaySound(pszSound, 0, uFlags, TRUE);
563 /**************************************************************************
564 * mmsystemGetVersion [WINMM.@]
566 UINT WINAPI mmsystemGetVersion(void)
568 TRACE("3.10 (Win95?)\n");
569 return 0x030a;