2 * Copyright 1993 Martin Ayotte
3 * 1998-2002 Eric Pouech
4 * 2011 Andrew Eikum for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
43 #include "mmdeviceapi.h"
44 #include "audioclient.h"
45 #include "audiopolicy.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
51 /* FIXME: Should be localized */
52 static const WCHAR volumeW[] = {'V','o','l','u','m','e',0};
53 static const WCHAR mastervolumeW[] = {'M','a','s','t','e','r',' ','V','o','l',
55 static const WCHAR muteW[] = {'M','u','t','e',0};
57 /* HWAVE (and HMIXER) format:
59 * XXXX... 1FDD DDDD IIII IIII
60 * X = unused (must be 0)
61 * 1 = the bit is set to 1, to avoid all-zero HWAVEs
62 * F = flow direction (0 = IN, 1 = OUT)
63 * D = index into g_out_mmdevices
64 * I = index in the mmdevice's devices array
66 * Two reasons that we don't just use pointers:
67 * - HWAVEs must fit into 16 bits for compatibility with old applications.
68 * - We must be able to identify bad devices without crashing.
71 #define MAX_DEVICES 256
73 typedef struct _WINMM_CBInfo {
80 struct _WINMM_MMDevice;
81 typedef struct _WINMM_MMDevice WINMM_MMDevice;
83 typedef struct _WINMM_Device {
92 IAudioRenderClient *render;
93 IAudioCaptureClient *capture;
95 IAudioStreamVolume *volume;
97 HACMSTREAM acm_handle;
98 ACMSTREAMHEADER acm_hdr;
101 WAVEHDR *first, *last, *playing, *loop_start;
105 UINT32 bytes_per_frame, samples_per_sec, ofs_bytes, played_frames;
107 /* stored in frames of sample rate, *not* AC::GetFrequency */
108 UINT64 last_clock_pos;
111 CRITICAL_SECTION lock;
113 WINMM_MMDevice *parent;
116 struct _WINMM_MMDevice {
117 WAVEOUTCAPSW out_caps; /* must not be modified outside of WINMM_InitMMDevices*/
118 WAVEINCAPSW in_caps; /* must not be modified outside of WINMM_InitMMDevices*/
121 ISimpleAudioVolume *volume;
125 /* HMIXER format is the same as the HWAVE format, but the I bits are
126 * replaced by the value of this counter, to keep each HMIXER unique */
129 CRITICAL_SECTION lock;
131 WINMM_Device *devices[MAX_DEVICES];
134 static WINMM_MMDevice *g_out_mmdevices;
135 static UINT g_outmmdevices_count;
137 static WINMM_MMDevice *g_in_mmdevices;
138 static UINT g_inmmdevices_count;
140 static IMMDeviceEnumerator *g_devenum;
142 static CRITICAL_SECTION g_devthread_lock;
143 static HANDLE g_devices_thread;
144 static HWND g_devices_hwnd;
146 static UINT g_devhandle_count;
147 static HANDLE *g_device_handles;
148 static WINMM_Device **g_handle_devices;
150 typedef struct _WINMM_OpenInfo {
153 WAVEFORMATEX *format;
159 typedef struct _WINMM_ControlDetails {
161 MIXERCONTROLDETAILS *details;
163 } WINMM_ControlDetails;
165 static LRESULT WOD_Open(WINMM_OpenInfo *info);
166 static LRESULT WOD_Close(HWAVEOUT hwave);
167 static LRESULT WID_Open(WINMM_OpenInfo *info);
168 static LRESULT WID_Close(HWAVEIN hwave);
170 BOOL WINMM_InitWaveform(void)
172 InitializeCriticalSection(&g_devthread_lock);
176 static inline HWAVE WINMM_MakeHWAVE(UINT mmdevice, BOOL is_out, UINT device)
178 return ULongToHandle((1 << 15) | ((!!is_out) << 14) |
179 (mmdevice << 8) | device);
182 static inline void WINMM_DecomposeHWAVE(HWAVE hwave, UINT *mmdevice_index,
183 BOOL *is_out, UINT *device_index, UINT *junk)
185 ULONG32 l = HandleToULong(hwave);
186 *device_index = l & 0xFF;
187 *mmdevice_index = (l >> 8) & 0x3F;
188 *is_out = (l >> 14) & 0x1;
192 static void WINMM_InitDevice(WINMM_Device *device,
193 WINMM_MMDevice *parent, HWAVE hwave)
195 InitializeCriticalSection(&device->lock);
196 device->handle = hwave;
197 device->parent = parent;
200 /* finds the first unused Device, marks it as "open", and returns
201 * a pointer to the device
203 * IMPORTANT: it is the caller's responsibility to release the device's lock
206 static WINMM_Device *WINMM_FindUnusedDevice(BOOL is_out, UINT mmdevice_index)
208 WINMM_MMDevice *mmdevice;
212 mmdevice = &g_out_mmdevices[mmdevice_index];
214 mmdevice = &g_in_mmdevices[mmdevice_index];
216 EnterCriticalSection(&mmdevice->lock);
217 for(i = 0; i < MAX_DEVICES; ++i){
218 WINMM_Device *device = mmdevice->devices[i];
221 device = mmdevice->devices[i] = HeapAlloc(GetProcessHeap(),
222 HEAP_ZERO_MEMORY, sizeof(WINMM_Device));
224 LeaveCriticalSection(&mmdevice->lock);
228 WINMM_InitDevice(device, mmdevice,
229 WINMM_MakeHWAVE(mmdevice_index, is_out, i));
230 EnterCriticalSection(&device->lock);
232 EnterCriticalSection(&device->lock);
235 LeaveCriticalSection(&mmdevice->lock);
237 TRACE("Found free device: mmdevice: %u, device id: %u\n",
242 LeaveCriticalSection(&device->lock);
245 LeaveCriticalSection(&mmdevice->lock);
247 TRACE("All devices in use: mmdevice: %u\n", mmdevice_index);
252 static inline BOOL WINMM_ValidateAndLock(WINMM_Device *device)
257 EnterCriticalSection(&device->lock);
260 LeaveCriticalSection(&device->lock);
267 static WINMM_Device *WINMM_GetDeviceFromHWAVE(HWAVE hwave)
269 WINMM_MMDevice *mmdevice;
270 WINMM_Device *device;
271 UINT mmdevice_index, device_index, junk;
274 WINMM_DecomposeHWAVE(hwave, &mmdevice_index, &is_out, &device_index, &junk);
279 if(mmdevice_index >= (is_out ? g_outmmdevices_count : g_inmmdevices_count))
283 mmdevice = &g_out_mmdevices[mmdevice_index];
285 mmdevice = &g_in_mmdevices[mmdevice_index];
287 EnterCriticalSection(&mmdevice->lock);
289 device = mmdevice->devices[device_index];
291 LeaveCriticalSection(&mmdevice->lock);
296 /* Note: NotifyClient should never be called while holding the device lock
297 * since the client may call wave* functions from within the callback. */
298 static inline void WINMM_NotifyClient(WINMM_CBInfo *info, WORD msg, DWORD_PTR param1,
301 DriverCallback(info->callback, info->flags, (HDRVR)info->hwave,
302 msg, info->user, param1, param2);
305 static HRESULT WINMM_GetFriendlyName(IMMDevice *device, WCHAR *out,
312 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
316 PropVariantInit(&var);
318 hr = IPropertyStore_GetValue(ps,
319 (PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &var);
321 IPropertyStore_Release(ps);
325 lstrcpynW(out, var.u.pwszVal, outlen);
327 PropVariantClear(&var);
329 IPropertyStore_Release(ps);
334 static HRESULT WINMM_TestFormat(IAudioClient *client, DWORD rate, DWORD depth,
337 WAVEFORMATEX fmt, *junk;
340 fmt.wFormatTag = WAVE_FORMAT_PCM;
341 fmt.nChannels = channels;
342 fmt.nSamplesPerSec = rate;
343 fmt.wBitsPerSample = depth;
344 fmt.nBlockAlign = (channels * depth) / 8;
345 fmt.nAvgBytesPerSec = rate * fmt.nBlockAlign;
348 hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED,
356 static struct _TestFormat {
361 } formats_to_test[] = {
362 { WAVE_FORMAT_1M08, 11025, 8, 1 },
363 { WAVE_FORMAT_1M16, 11025, 16, 1 },
364 { WAVE_FORMAT_1S08, 11025, 8, 2 },
365 { WAVE_FORMAT_1S16, 11025, 16, 2 },
366 { WAVE_FORMAT_2M08, 22050, 8, 1 },
367 { WAVE_FORMAT_2M16, 22050, 16, 1 },
368 { WAVE_FORMAT_2S08, 22050, 8, 2 },
369 { WAVE_FORMAT_2S16, 22050, 16, 2 },
370 { WAVE_FORMAT_4M08, 44100, 8, 1 },
371 { WAVE_FORMAT_4M16, 44100, 16, 1 },
372 { WAVE_FORMAT_4S08, 44100, 8, 2 },
373 { WAVE_FORMAT_4S16, 44100, 16, 2 },
374 { WAVE_FORMAT_48M08, 48000, 8, 1 },
375 { WAVE_FORMAT_48M16, 48000, 16, 1 },
376 { WAVE_FORMAT_48S08, 48000, 8, 2 },
377 { WAVE_FORMAT_48S16, 48000, 16, 2 },
378 { WAVE_FORMAT_96M08, 96000, 8, 1 },
379 { WAVE_FORMAT_96M16, 96000, 16, 1 },
380 { WAVE_FORMAT_96S08, 96000, 8, 2 },
381 { WAVE_FORMAT_96S16, 96000, 16, 2 },
385 static DWORD WINMM_GetSupportedFormats(IMMDevice *device)
389 struct _TestFormat *fmt;
390 IAudioClient *client;
392 hr = IMMDevice_Activate(device, &IID_IAudioClient,
393 CLSCTX_INPROC_SERVER, NULL, (void**)&client);
397 for(fmt = formats_to_test; fmt->flag; ++fmt){
398 hr = WINMM_TestFormat(client, fmt->rate, fmt->depth, fmt->channels);
403 IAudioClient_Release(client);
408 static HRESULT WINMM_InitMMDevice(EDataFlow flow, IMMDevice *device,
409 WINMM_MMDevice *dev, UINT index)
414 dev->out_caps.wMid = 0xFF;
415 dev->out_caps.wPid = 0xFF;
416 dev->out_caps.vDriverVersion = 0x00010001;
417 dev->out_caps.dwFormats = WINMM_GetSupportedFormats(device);
418 dev->out_caps.wReserved1 = 0;
419 dev->out_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
420 WAVECAPS_SAMPLEACCURATE;
421 dev->out_caps.wChannels = 2;
422 dev->out_caps.szPname[0] = '\0';
424 hr = WINMM_GetFriendlyName(device, dev->out_caps.szPname,
425 sizeof(dev->out_caps.szPname) /
426 sizeof(*dev->out_caps.szPname));
430 dev->in_caps.wMid = 0xFF;
431 dev->in_caps.wPid = 0xFF;
432 dev->in_caps.vDriverVersion = 0x00010001;
433 dev->in_caps.dwFormats = WINMM_GetSupportedFormats(device);
434 dev->in_caps.wReserved1 = 0;
435 dev->in_caps.wChannels = 2;
436 dev->in_caps.szPname[0] = '\0';
438 hr = WINMM_GetFriendlyName(device, dev->in_caps.szPname,
439 sizeof(dev->in_caps.szPname) /
440 sizeof(*dev->in_caps.szPname));
445 hr = IMMDevice_GetId(device, &dev->dev_id);
449 CoCreateGuid(&dev->session);
451 InitializeCriticalSection(&dev->lock);
456 static HRESULT WINMM_EnumDevices(WINMM_MMDevice **devices, UINT *devcount,
459 IMMDeviceCollection *devcoll;
462 hr = IMMDeviceEnumerator_EnumAudioEndpoints(g_devenum, flow,
463 DEVICE_STATE_ACTIVE, &devcoll);
467 hr = IMMDeviceCollection_GetCount(devcoll, devcount);
469 IMMDeviceCollection_Release(devcoll);
475 IMMDevice *def_dev = NULL;
477 *devices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
478 sizeof(WINMM_MMDevice) * (*devcount));
480 IMMDeviceCollection_Release(devcoll);
481 return E_OUTOFMEMORY;
486 /* make sure that device 0 is the default device */
487 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(g_devenum,
488 flow, eConsole, &def_dev);
490 WINMM_InitMMDevice(flow, def_dev, &(*devices)[0], 0);
494 for(n = 0; n < *devcount; ++n){
497 hr = IMMDeviceCollection_Item(devcoll, n, &device);
499 if(device != def_dev){
500 WINMM_InitMMDevice(flow, device, &(*devices)[count], count);
504 IMMDevice_Release(device);
509 IMMDevice_Release(def_dev);
514 IMMDeviceCollection_Release(devcoll);
519 static HRESULT WINMM_InitMMDevices(void)
523 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
524 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&g_devenum);
528 hr = WINMM_EnumDevices(&g_out_mmdevices, &g_outmmdevices_count, eRender);
530 g_outmmdevices_count = 0;
531 g_inmmdevices_count = 0;
535 hr = WINMM_EnumDevices(&g_in_mmdevices, &g_inmmdevices_count, eCapture);
537 g_inmmdevices_count = 0;
544 static inline BOOL WINMM_IsMapper(UINT device)
546 return (device == WAVE_MAPPER || device == (UINT16)WAVE_MAPPER);
549 static MMRESULT WINMM_TryDeviceMapping(WINMM_OpenInfo *info, WORD channels,
550 DWORD freq, DWORD bits_per_samp, BOOL is_out)
552 WINMM_Device *device;
557 TRACE("format: %u, channels: %u, sample rate: %u, bit depth: %u\n",
558 WAVE_FORMAT_PCM, channels, freq, bits_per_samp);
560 target.wFormatTag = WAVE_FORMAT_PCM;
561 target.nChannels = channels;
562 target.nSamplesPerSec = freq;
563 target.wBitsPerSample = bits_per_samp;
564 target.nBlockAlign = (target.nChannels * target.wBitsPerSample) / 8;
565 target.nAvgBytesPerSec = target.nSamplesPerSec * target.nBlockAlign;
569 mr = acmStreamOpen(NULL, NULL, info->format, &target, NULL, 0,
570 0, ACM_STREAMOPENF_QUERY);
572 mr = acmStreamOpen(NULL, NULL, &target, info->format, NULL, 0,
573 0, ACM_STREAMOPENF_QUERY);
574 if(mr != MMSYSERR_NOERROR)
577 /* ACM can convert from src->dst, so try to find a device
578 * that supports dst */
580 if(WINMM_IsMapper(info->req_device)){
581 for(i = 0; i < g_outmmdevices_count; ++i){
582 WINMM_OpenInfo l_info = *info;
583 l_info.req_device = i;
584 l_info.format = ⌖
585 mr = WOD_Open(&l_info);
586 if(mr == MMSYSERR_NOERROR){
587 info->handle = l_info.handle;
592 WINMM_OpenInfo l_info = *info;
593 l_info.flags &= ~WAVE_MAPPED;
594 l_info.format = ⌖
595 mr = WOD_Open(&l_info);
596 if(mr == MMSYSERR_NOERROR)
597 info->handle = l_info.handle;
600 if(WINMM_IsMapper(info->req_device)){
601 for(i = 0; i < g_inmmdevices_count; ++i){
602 WINMM_OpenInfo l_info = *info;
603 l_info.req_device = i;
604 l_info.format = ⌖
605 mr = WID_Open(&l_info);
606 if(mr == MMSYSERR_NOERROR){
607 info->handle = l_info.handle;
612 WINMM_OpenInfo l_info = *info;
613 l_info.flags &= ~WAVE_MAPPED;
614 l_info.format = ⌖
615 mr = WID_Open(&l_info);
616 if(mr == MMSYSERR_NOERROR)
617 info->handle = l_info.handle;
620 if(mr != MMSYSERR_NOERROR)
621 return WAVERR_BADFORMAT;
623 device = WINMM_GetDeviceFromHWAVE(info->handle);
625 return MMSYSERR_INVALHANDLE;
627 /* set up the ACM stream */
629 mr = acmStreamOpen(&device->acm_handle, NULL, info->format, &target,
632 mr = acmStreamOpen(&device->acm_handle, NULL, &target, info->format,
634 if(mr != MMSYSERR_NOERROR){
636 WOD_Close((HWAVEOUT)info->handle);
638 WID_Close((HWAVEIN)info->handle);
643 return MMSYSERR_NOERROR;
646 static MMRESULT WINMM_MapDevice(WINMM_OpenInfo *info, BOOL is_out)
650 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)info->format;
652 TRACE("(%p, %d)\n", info, is_out);
654 /* try to find a direct match */
656 WINMM_OpenInfo l_info = *info;
657 if(WINMM_IsMapper(info->req_device)){
658 for(i = 0; i < g_outmmdevices_count; ++i){
659 l_info.req_device = i;
660 mr = WOD_Open(&l_info);
661 if(mr == MMSYSERR_NOERROR){
662 info->handle = l_info.handle;
667 l_info.flags &= ~WAVE_MAPPED;
668 mr = WOD_Open(&l_info);
669 if(mr == MMSYSERR_NOERROR){
670 info->handle = l_info.handle;
675 WINMM_OpenInfo l_info = *info;
676 if(WINMM_IsMapper(info->req_device)){
677 for(i = 0; i < g_inmmdevices_count; ++i){
678 l_info.req_device = i;
679 mr = WID_Open(&l_info);
680 if(mr == MMSYSERR_NOERROR){
681 info->handle = l_info.handle;
686 l_info.flags &= ~WAVE_MAPPED;
687 mr = WID_Open(&l_info);
688 if(mr == MMSYSERR_NOERROR){
689 info->handle = l_info.handle;
695 /* no direct match, so set up the ACM stream */
696 if(info->format->wFormatTag != WAVE_FORMAT_PCM &&
697 !(info->format->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
698 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
699 /* convert to PCM format if it's not already */
700 mr = WINMM_TryDeviceMapping(info, info->format->nChannels,
701 info->format->nSamplesPerSec, 16, is_out);
702 if(mr == MMSYSERR_NOERROR)
705 mr = WINMM_TryDeviceMapping(info, info->format->nChannels,
706 info->format->nSamplesPerSec, 8, is_out);
707 if(mr == MMSYSERR_NOERROR)
712 /* first try just changing bit depth and channels */
713 channels = info->format->nChannels;
714 mr = WINMM_TryDeviceMapping(info, channels,
715 info->format->nSamplesPerSec, 16, is_out);
716 if(mr == MMSYSERR_NOERROR)
718 mr = WINMM_TryDeviceMapping(info, channels,
719 info->format->nSamplesPerSec, 8, is_out);
720 if(mr == MMSYSERR_NOERROR)
723 channels = (channels == 2) ? 1 : 2;
724 mr = WINMM_TryDeviceMapping(info, channels,
725 info->format->nSamplesPerSec, 16, is_out);
726 if(mr == MMSYSERR_NOERROR)
728 mr = WINMM_TryDeviceMapping(info, channels,
729 info->format->nSamplesPerSec, 8, is_out);
730 if(mr == MMSYSERR_NOERROR)
733 /* that didn't work, so now try different sample rates */
734 channels = info->format->nChannels;
735 mr = WINMM_TryDeviceMapping(info, channels, 96000, 16, is_out);
736 if(mr == MMSYSERR_NOERROR)
738 mr = WINMM_TryDeviceMapping(info, channels, 48000, 16, is_out);
739 if(mr == MMSYSERR_NOERROR)
741 mr = WINMM_TryDeviceMapping(info, channels, 44100, 16, is_out);
742 if(mr == MMSYSERR_NOERROR)
744 mr = WINMM_TryDeviceMapping(info, channels, 22050, 16, is_out);
745 if(mr == MMSYSERR_NOERROR)
747 mr = WINMM_TryDeviceMapping(info, channels, 11025, 16, is_out);
748 if(mr == MMSYSERR_NOERROR)
751 channels = (channels == 2) ? 1 : 2;
752 mr = WINMM_TryDeviceMapping(info, channels, 96000, 16, is_out);
753 if(mr == MMSYSERR_NOERROR)
755 mr = WINMM_TryDeviceMapping(info, channels, 48000, 16, is_out);
756 if(mr == MMSYSERR_NOERROR)
758 mr = WINMM_TryDeviceMapping(info, channels, 44100, 16, is_out);
759 if(mr == MMSYSERR_NOERROR)
761 mr = WINMM_TryDeviceMapping(info, channels, 22050, 16, is_out);
762 if(mr == MMSYSERR_NOERROR)
764 mr = WINMM_TryDeviceMapping(info, channels, 11025, 16, is_out);
765 if(mr == MMSYSERR_NOERROR)
768 channels = info->format->nChannels;
769 mr = WINMM_TryDeviceMapping(info, channels, 96000, 8, is_out);
770 if(mr == MMSYSERR_NOERROR)
772 mr = WINMM_TryDeviceMapping(info, channels, 48000, 8, is_out);
773 if(mr == MMSYSERR_NOERROR)
775 mr = WINMM_TryDeviceMapping(info, channels, 44100, 8, is_out);
776 if(mr == MMSYSERR_NOERROR)
778 mr = WINMM_TryDeviceMapping(info, channels, 22050, 8, is_out);
779 if(mr == MMSYSERR_NOERROR)
781 mr = WINMM_TryDeviceMapping(info, channels, 11025, 8, is_out);
782 if(mr == MMSYSERR_NOERROR)
785 channels = (channels == 2) ? 1 : 2;
786 mr = WINMM_TryDeviceMapping(info, channels, 96000, 8, is_out);
787 if(mr == MMSYSERR_NOERROR)
789 mr = WINMM_TryDeviceMapping(info, channels, 48000, 8, is_out);
790 if(mr == MMSYSERR_NOERROR)
792 mr = WINMM_TryDeviceMapping(info, channels, 44100, 8, is_out);
793 if(mr == MMSYSERR_NOERROR)
795 mr = WINMM_TryDeviceMapping(info, channels, 22050, 8, is_out);
796 if(mr == MMSYSERR_NOERROR)
798 mr = WINMM_TryDeviceMapping(info, channels, 11025, 8, is_out);
799 if(mr == MMSYSERR_NOERROR)
803 WARN("Unable to find compatible device!\n");
804 return WAVERR_BADFORMAT;
807 static LRESULT WINMM_OpenDevice(WINMM_Device *device, WINMM_MMDevice *mmdevice,
808 WINMM_OpenInfo *info)
810 WAVEFORMATEX *closer_fmt = NULL, fmt, *passed_fmt;
811 LRESULT ret = MMSYSERR_ERROR;
814 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id,
817 ERR("Device %s (%s) unavailable: %08x\n",
818 wine_dbgstr_w(mmdevice->dev_id),
819 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
823 hr = IMMDevice_Activate(device->device, &IID_IAudioClient,
824 CLSCTX_INPROC_SERVER, NULL, (void**)&device->client);
826 ERR("Activate failed: %08x\n", hr);
830 if(info->format->wFormatTag == WAVE_FORMAT_PCM){
831 /* we aren't guaranteed that the struct in lpFormat is a full
832 * WAVEFORMATEX struct, which IAC::IsFormatSupported requires */
834 memcpy(passed_fmt, info->format, sizeof(PCMWAVEFORMAT));
835 passed_fmt->cbSize = 0;
837 passed_fmt = info->format;
839 hr = IAudioClient_IsFormatSupported(device->client,
840 AUDCLNT_SHAREMODE_SHARED, passed_fmt, &closer_fmt);
842 CoTaskMemFree(closer_fmt);
843 if(FAILED(hr) && hr != AUDCLNT_E_UNSUPPORTED_FORMAT){
844 ERR("IsFormatSupported failed: %08x\n", hr);
847 if(hr == S_FALSE || hr == AUDCLNT_E_UNSUPPORTED_FORMAT){
848 ret = WAVERR_BADFORMAT;
851 if(info->flags & WAVE_FORMAT_QUERY){
852 ret = MMSYSERR_NOERROR;
856 /* buffer size = 10 * 100000 (100 ns) = 0.1 seconds */
857 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
858 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
859 10 * 100000, 50000, passed_fmt, &device->parent->session);
861 ERR("Initialize failed: %08x\n", hr);
865 hr = IAudioClient_GetService(device->client, &IID_IAudioClock,
866 (void**)&device->clock);
868 ERR("GetService failed: %08x\n", hr);
873 device->event = CreateEventW(NULL, FALSE, FALSE, NULL);
875 ERR("CreateEvent failed: %08x\n", hr);
879 /* As the devices thread is waiting on g_device_handles, it can
880 * only be modified from within this same thread. */
881 if(g_device_handles){
882 g_device_handles = HeapReAlloc(GetProcessHeap(), 0, g_device_handles,
883 sizeof(HANDLE) * (g_devhandle_count + 1));
884 g_handle_devices = HeapReAlloc(GetProcessHeap(), 0, g_handle_devices,
885 sizeof(WINMM_Device *) * (g_devhandle_count + 1));
887 g_device_handles = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE));
888 g_handle_devices = HeapAlloc(GetProcessHeap(), 0,
889 sizeof(WINMM_Device *));
891 g_device_handles[g_devhandle_count] = device->event;
892 g_handle_devices[g_devhandle_count] = device;
896 hr = IAudioClient_SetEventHandle(device->client, device->event);
898 ERR("SetEventHandle failed: %08x\n", hr);
902 device->bytes_per_frame = info->format->nBlockAlign;
903 device->samples_per_sec = info->format->nSamplesPerSec;
905 device->played_frames = 0;
906 device->last_clock_pos = 0;
907 device->ofs_bytes = 0;
908 device->loop_counter = 0;
909 device->stopped = TRUE;
910 device->first = device->last = device->playing = device->loop_start = NULL;
912 device->cb_info.flags = HIWORD(info->flags & CALLBACK_TYPEMASK);
913 device->cb_info.callback = info->callback;
914 device->cb_info.user = info->cb_user;
915 device->cb_info.hwave = device->handle;
917 info->handle = device->handle;
919 return MMSYSERR_NOERROR;
923 IAudioClient_Release(device->client);
924 device->client = NULL;
927 IMMDevice_Release(device->device);
928 device->device = NULL;
934 static LRESULT WOD_Open(WINMM_OpenInfo *info)
936 WINMM_MMDevice *mmdevice;
937 WINMM_Device *device = NULL;
938 LRESULT ret = MMSYSERR_ERROR;
941 TRACE("(%u, %p, %08x)\n", info->req_device, info, info->flags);
943 if(WINMM_IsMapper(info->req_device) || (info->flags & WAVE_MAPPED))
944 return WINMM_MapDevice(info, TRUE);
946 if(info->req_device >= g_outmmdevices_count)
947 return MMSYSERR_BADDEVICEID;
949 mmdevice = &g_out_mmdevices[info->req_device];
951 if(!mmdevice->out_caps.szPname[0])
952 return MMSYSERR_NOTENABLED;
954 device = WINMM_FindUnusedDevice(TRUE, info->req_device);
956 return MMSYSERR_ALLOCATED;
958 ret = WINMM_OpenDevice(device, mmdevice, info);
959 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
961 ret = MMSYSERR_ERROR;
963 hr = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
964 (void**)&device->render);
966 ERR("GetService failed: %08x\n", hr);
970 hr = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
971 (void**)&device->volume);
973 ERR("GetService failed: %08x\n", hr);
977 LeaveCriticalSection(&device->lock);
979 return MMSYSERR_NOERROR;
983 IMMDevice_Release(device->device);
984 device->device = NULL;
987 IAudioClient_Release(device->client);
988 device->client = NULL;
991 IAudioRenderClient_Release(device->render);
992 device->render = NULL;
995 IAudioStreamVolume_Release(device->volume);
996 device->volume = NULL;
999 IAudioClock_Release(device->clock);
1000 device->clock = NULL;
1002 device->open = FALSE;
1003 LeaveCriticalSection(&device->lock);
1007 static LRESULT WID_Open(WINMM_OpenInfo *info)
1009 WINMM_MMDevice *mmdevice;
1010 WINMM_Device *device = NULL;
1011 LRESULT ret = MMSYSERR_ERROR;
1014 TRACE("(%u, %p, %08x)\n", info->req_device, info, info->flags);
1016 if(WINMM_IsMapper(info->req_device) || info->flags & WAVE_MAPPED)
1017 return WINMM_MapDevice(info, FALSE);
1019 if(info->req_device >= g_inmmdevices_count)
1020 return MMSYSERR_BADDEVICEID;
1022 mmdevice = &g_in_mmdevices[info->req_device];
1024 if(!mmdevice->in_caps.szPname[0])
1025 return MMSYSERR_NOTENABLED;
1027 device = WINMM_FindUnusedDevice(FALSE, info->req_device);
1029 return MMSYSERR_ALLOCATED;
1031 ret = WINMM_OpenDevice(device, mmdevice, info);
1032 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1034 ret = MMSYSERR_ERROR;
1036 hr = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
1037 (void**)&device->capture);
1039 ERR("GetService failed: %08x\n", hr);
1043 LeaveCriticalSection(&device->lock);
1045 return MMSYSERR_NOERROR;
1049 IMMDevice_Release(device->device);
1050 device->device = NULL;
1053 IAudioClient_Release(device->client);
1054 device->client = NULL;
1056 if(device->capture){
1057 IAudioCaptureClient_Release(device->capture);
1058 device->capture = NULL;
1061 IAudioClock_Release(device->clock);
1062 device->clock = NULL;
1064 device->open = FALSE;
1065 LeaveCriticalSection(&device->lock);
1069 static HRESULT WINMM_CloseDevice(WINMM_Device *device)
1071 device->open = FALSE;
1073 if(!device->stopped){
1074 IAudioClient_Stop(device->client);
1075 device->stopped = TRUE;
1078 if(device->acm_handle){
1079 acmStreamClose(device->acm_handle, 0);
1080 device->acm_handle = NULL;
1083 IMMDevice_Release(device->device);
1084 device->device = NULL;
1086 IAudioClient_Release(device->client);
1087 device->client = NULL;
1089 IAudioClock_Release(device->clock);
1090 device->clock = NULL;
1095 static LRESULT WOD_Close(HWAVEOUT hwave)
1097 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1099 TRACE("(%p)\n", hwave);
1101 if(!WINMM_ValidateAndLock(device))
1102 return MMSYSERR_INVALHANDLE;
1104 WINMM_CloseDevice(device);
1106 IAudioRenderClient_Release(device->render);
1107 device->render = NULL;
1109 IAudioStreamVolume_Release(device->volume);
1110 device->volume = NULL;
1112 LeaveCriticalSection(&device->lock);
1114 return MMSYSERR_NOERROR;
1117 static LRESULT WID_Close(HWAVEIN hwave)
1119 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1121 TRACE("(%p)\n", hwave);
1123 if(!WINMM_ValidateAndLock(device))
1124 return MMSYSERR_INVALHANDLE;
1126 WINMM_CloseDevice(device);
1128 IAudioCaptureClient_Release(device->capture);
1129 device->capture = NULL;
1131 LeaveCriticalSection(&device->lock);
1133 return MMSYSERR_NOERROR;
1136 static LRESULT WINMM_PrepareHeader(HWAVE hwave, WAVEHDR *header)
1138 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1140 TRACE("(%p, %p)\n", hwave, header);
1142 if(!WINMM_ValidateAndLock(device))
1143 return MMSYSERR_INVALHANDLE;
1145 if(device->render && device->acm_handle){
1146 ACMSTREAMHEADER *ash;
1150 mr = acmStreamSize(device->acm_handle, header->dwBufferLength, &size,
1151 ACM_STREAMSIZEF_SOURCE);
1152 if(mr != MMSYSERR_NOERROR){
1153 LeaveCriticalSection(&device->lock);
1157 ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + size);
1159 LeaveCriticalSection(&device->lock);
1160 return MMSYSERR_NOMEM;
1163 ash->cbStruct = sizeof(*ash);
1165 ash->dwUser = (DWORD_PTR)header;
1166 ash->pbSrc = (BYTE*)header->lpData;
1167 ash->cbSrcLength = header->dwBufferLength;
1168 ash->dwSrcUser = header->dwUser;
1169 ash->pbDst = (BYTE*)ash + sizeof(ACMSTREAMHEADER);
1170 ash->cbDstLength = size;
1173 mr = acmStreamPrepareHeader(device->acm_handle, ash, 0);
1174 if(mr != MMSYSERR_NOERROR){
1175 HeapFree(GetProcessHeap(), 0, ash);
1176 LeaveCriticalSection(&device->lock);
1180 header->reserved = (DWORD_PTR)ash;
1183 LeaveCriticalSection(&device->lock);
1185 header->dwFlags |= WHDR_PREPARED;
1186 header->dwFlags &= ~WHDR_DONE;
1188 return MMSYSERR_NOERROR;
1191 static LRESULT WINMM_UnprepareHeader(HWAVE hwave, WAVEHDR *header)
1193 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1195 TRACE("(%p, %p)\n", hwave, header);
1197 if(!WINMM_ValidateAndLock(device))
1198 return MMSYSERR_INVALHANDLE;
1200 if(device->render && device->acm_handle){
1201 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1203 acmStreamUnprepareHeader(device->acm_handle, ash, 0);
1205 HeapFree(GetProcessHeap(), 0, ash);
1208 LeaveCriticalSection(&device->lock);
1210 header->dwFlags &= ~WHDR_PREPARED;
1211 header->dwFlags |= WHDR_DONE;
1213 return MMSYSERR_NOERROR;
1216 static UINT32 WINMM_HeaderLenBytes(WINMM_Device *device, WAVEHDR *header)
1218 if(device->acm_handle){
1219 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1220 return ash->cbDstLengthUsed;
1223 return header->dwBufferLength;
1226 static UINT32 WINMM_HeaderLenFrames(WINMM_Device *device, WAVEHDR *header)
1228 return WINMM_HeaderLenBytes(device, header) / device->bytes_per_frame;
1231 static WAVEHDR *WOD_MarkDoneHeaders(WINMM_Device *device)
1234 WAVEHDR *queue, *first = device->first;
1235 UINT64 clock_freq, clock_pos, clock_frames;
1236 UINT32 nloops, queue_frames = 0;
1238 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
1240 ERR("GetFrequency failed: %08x\n", hr);
1244 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
1246 ERR("GetPosition failed: %08x\n", hr);
1250 clock_frames = (clock_pos / (double)clock_freq) * device->samples_per_sec;
1252 first = queue = device->first;
1253 nloops = device->loop_counter;
1255 (queue_frames += WINMM_HeaderLenFrames(device, queue)) <=
1256 clock_frames - device->last_clock_pos){
1258 device->first->dwFlags &= ~WHDR_INQUEUE;
1259 device->first->dwFlags |= WHDR_DONE;
1260 device->last_clock_pos += queue_frames;
1262 queue = device->first = queue->lpNext;
1264 if(queue->dwFlags & WHDR_BEGINLOOP){
1265 if(queue->dwFlags & WHDR_ENDLOOP)
1268 queue = queue->lpNext;
1269 }else if(queue->dwFlags & WHDR_ENDLOOP){
1270 queue = device->loop_start;
1279 static void WOD_PushData(WINMM_Device *device)
1281 WINMM_CBInfo cb_info;
1283 UINT32 pad, bufsize, avail_frames, queue_frames, written, ofs;
1284 UINT32 queue_bytes, nloops;
1286 WAVEHDR *queue, *first = NULL;
1288 TRACE("(%p)\n", device->handle);
1290 EnterCriticalSection(&device->lock);
1296 device->stopped = TRUE;
1297 device->last_clock_pos = 0;
1298 IAudioClient_Stop(device->client);
1299 IAudioClient_Reset(device->client);
1303 hr = IAudioClient_GetBufferSize(device->client, &bufsize);
1305 ERR("GetBufferSize failed: %08x\n", hr);
1309 hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1311 ERR("GetCurrentPadding failed: %08x\n", hr);
1315 first = WOD_MarkDoneHeaders(device);
1317 /* determine which is larger between the available buffer size and
1318 * the amount of data left in the queue */
1319 avail_frames = bufsize - pad;
1321 queue = device->playing;
1322 ofs = device->ofs_bytes;
1325 while(queue && queue_frames < avail_frames){
1326 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1327 queue_frames += (queue_bytes - ofs) / device->bytes_per_frame;
1330 if(queue->dwFlags & WHDR_ENDLOOP && nloops < device->loop_counter){
1331 queue = device->loop_start;
1334 queue = queue->lpNext;
1337 if(avail_frames != 0 && queue_frames == 0){
1338 hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1340 ERR("GetBuffer failed: %08x\n", hr);
1344 hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames,
1345 AUDCLNT_BUFFERFLAGS_SILENT);
1347 ERR("ReleaseBuffer failed: %08x\n", hr);
1354 if(queue_frames < avail_frames)
1355 avail_frames = queue_frames;
1356 if(avail_frames == 0)
1359 hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1361 ERR("GetBuffer failed: %08x\n", hr);
1366 while(device->playing && written < avail_frames){
1367 UINT32 copy_frames, copy_bytes;
1370 queue = device->playing;
1372 if(device->acm_handle){
1373 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)queue->reserved;
1374 queue_bytes = ash->cbDstLengthUsed;
1375 queue_data = ash->pbDst;
1377 queue_bytes = queue->dwBufferLength;
1378 queue_data = (BYTE*)queue->lpData;
1381 queue_frames = (queue_bytes - device->ofs_bytes) /
1382 device->bytes_per_frame;
1384 copy_frames = queue_frames < (avail_frames - written) ?
1385 queue_frames : avail_frames - written;
1386 copy_bytes = copy_frames * device->bytes_per_frame;
1388 memcpy(data, queue_data + device->ofs_bytes, copy_bytes);
1391 written += copy_frames;
1392 device->ofs_bytes += copy_bytes;
1394 if(device->ofs_bytes >= queue_bytes){
1395 device->ofs_bytes = 0;
1397 if(!(queue->dwFlags & (WHDR_BEGINLOOP | WHDR_ENDLOOP)))
1398 device->playing = queue->lpNext;
1400 if(queue->dwFlags & WHDR_BEGINLOOP){
1401 device->loop_start = device->playing;
1402 device->playing = queue->lpNext;
1403 device->loop_counter = queue->dwLoops;
1405 if(queue->dwFlags & WHDR_ENDLOOP){
1406 --device->loop_counter;
1407 if(device->loop_counter)
1408 device->playing = device->loop_start;
1410 device->loop_start = device->playing = queue->lpNext;
1416 hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames, 0);
1418 ERR("ReleaseBuffer failed: %08x\n", hr);
1422 device->played_frames += avail_frames;
1425 cb_info = device->cb_info;
1427 LeaveCriticalSection(&device->lock);
1429 while(first && (first->dwFlags & WHDR_DONE)){
1430 WAVEHDR *next = first->lpNext;
1431 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1436 static void WID_PullACMData(WINMM_Device *device)
1438 UINT32 packet, packet_bytes;
1445 if(device->acm_hdr.cbDstLength == 0){
1446 hr = IAudioClient_GetCurrentPadding(device->client, &packet);
1448 ERR("GetCurrentPadding failed: %08x\n", hr);
1455 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1456 &flags, NULL, NULL);
1458 ERR("GetBuffer failed: %08x\n", hr);
1462 acmStreamSize(device->acm_handle, packet * device->bytes_per_frame,
1463 &packet_bytes, ACM_STREAMSIZEF_SOURCE);
1465 device->acm_offs = 0;
1467 device->acm_hdr.cbStruct = sizeof(device->acm_hdr);
1468 device->acm_hdr.fdwStatus = 0;
1469 device->acm_hdr.dwUser = 0;
1470 device->acm_hdr.pbSrc = data;
1471 device->acm_hdr.cbSrcLength = packet * device->bytes_per_frame;
1472 device->acm_hdr.cbSrcLengthUsed = 0;
1473 device->acm_hdr.dwSrcUser = 0;
1474 device->acm_hdr.pbDst = HeapAlloc(GetProcessHeap(), 0, packet_bytes);
1475 device->acm_hdr.cbDstLength = packet_bytes;
1476 device->acm_hdr.cbDstLengthUsed = 0;
1477 device->acm_hdr.dwDstUser = 0;
1479 mr = acmStreamPrepareHeader(device->acm_handle, &device->acm_hdr, 0);
1480 if(mr != MMSYSERR_NOERROR){
1481 ERR("acmStreamPrepareHeader failed: %d\n", mr);
1485 mr = acmStreamConvert(device->acm_handle, &device->acm_hdr, 0);
1486 if(mr != MMSYSERR_NOERROR){
1487 ERR("acmStreamConvert failed: %d\n", mr);
1491 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet);
1493 ERR("ReleaseBuffer failed: %08x\n", hr);
1496 queue = device->first;
1498 UINT32 to_copy_bytes;
1500 to_copy_bytes = min(queue->dwBufferLength - queue->dwBytesRecorded,
1501 device->acm_hdr.cbDstLengthUsed - device->acm_offs);
1503 memcpy(queue->lpData + queue->dwBytesRecorded,
1504 device->acm_hdr.pbDst + device->acm_offs, to_copy_bytes);
1506 queue->dwBytesRecorded += to_copy_bytes;
1507 device->acm_offs += to_copy_bytes;
1509 if(queue->dwBufferLength - queue->dwBytesRecorded <
1510 device->bytes_per_frame){
1511 queue->dwFlags &= ~WHDR_INQUEUE;
1512 queue->dwFlags |= WHDR_DONE;
1513 device->first = queue = queue->lpNext;
1516 if(device->acm_offs >= device->acm_hdr.cbDstLengthUsed){
1517 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1518 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1519 device->acm_hdr.cbDstLength = 0;
1520 device->acm_hdr.cbDstLengthUsed = 0;
1522 /* done with this ACM Header, so try to pull more data */
1523 WID_PullACMData(device);
1528 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1529 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1530 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1531 device->acm_hdr.cbDstLength = 0;
1532 device->acm_hdr.cbDstLengthUsed = 0;
1535 static void WID_PullData(WINMM_Device *device)
1537 WINMM_CBInfo cb_info;
1538 WAVEHDR *queue, *first = NULL;
1541 TRACE("(%p)\n", device->handle);
1543 EnterCriticalSection(&device->lock);
1545 if(!device->device || !device->first)
1548 first = device->first;
1550 if(device->acm_handle){
1551 WID_PullACMData(device);
1555 while(device->first){
1557 UINT32 pad, packet_len, packet;
1560 hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1562 ERR("GetCurrentPadding failed: %08x\n", hr);
1569 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1570 &flags, NULL, NULL);
1572 ERR("GetBuffer failed: %08x\n", hr);
1576 packet_len = packet;
1577 queue = device->first;
1578 while(queue && packet > 0){
1579 UINT32 to_copy_bytes;
1581 to_copy_bytes = min(packet * device->bytes_per_frame,
1582 queue->dwBufferLength - queue->dwBytesRecorded);
1584 memcpy(queue->lpData + queue->dwBytesRecorded, data,
1587 queue->dwBytesRecorded += to_copy_bytes;
1589 if(queue->dwBufferLength - queue->dwBytesRecorded <
1590 device->bytes_per_frame){
1591 queue->dwFlags &= ~WHDR_INQUEUE;
1592 queue->dwFlags |= WHDR_DONE;
1593 device->first = queue = queue->lpNext;
1596 packet -= to_copy_bytes / device->bytes_per_frame;
1599 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_len);
1601 ERR("ReleaseBuffer failed: %08x\n", hr);
1605 cb_info = device->cb_info;
1607 LeaveCriticalSection(&device->lock);
1609 while(first && (first->dwFlags & WHDR_DONE)){
1610 WAVEHDR *next = first->lpNext;
1611 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1616 static HRESULT WINMM_BeginPlaying(WINMM_Device *device)
1620 TRACE("(%p)\n", device->handle);
1623 /* prebuffer data before starting */
1624 WOD_PushData(device);
1626 if(device->stopped){
1627 device->stopped = FALSE;
1629 hr = IAudioClient_Start(device->client);
1630 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
1631 device->stopped = TRUE;
1632 ERR("Start failed: %08x\n", hr);
1640 static LRESULT WINMM_Pause(HWAVE hwave)
1642 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1645 TRACE("(%p)\n", hwave);
1647 if(!WINMM_ValidateAndLock(device))
1648 return MMSYSERR_INVALHANDLE;
1650 hr = IAudioClient_Stop(device->client);
1652 LeaveCriticalSection(&device->lock);
1653 ERR("Stop failed: %08x\n", hr);
1654 return MMSYSERR_ERROR;
1657 device->stopped = FALSE;
1659 LeaveCriticalSection(&device->lock);
1661 return MMSYSERR_NOERROR;
1664 static LRESULT WINMM_Reset(HWAVE hwave)
1666 WINMM_CBInfo cb_info;
1667 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1672 TRACE("(%p)\n", hwave);
1674 if(!WINMM_ValidateAndLock(device))
1675 return MMSYSERR_INVALHANDLE;
1677 hr = IAudioClient_Stop(device->client);
1679 LeaveCriticalSection(&device->lock);
1680 ERR("Stop failed: %08x\n", hr);
1681 return MMSYSERR_ERROR;
1683 device->stopped = TRUE;
1685 first = device->first;
1686 device->first = device->last = device->playing = NULL;
1687 device->ofs_bytes = 0;
1688 device->played_frames = 0;
1689 device->loop_counter = 0;
1690 device->last_clock_pos = 0;
1691 IAudioClient_Reset(device->client);
1693 cb_info = device->cb_info;
1694 is_out = device->render ? TRUE : FALSE;
1696 LeaveCriticalSection(&device->lock);
1699 WAVEHDR *next = first->lpNext;
1700 first->dwFlags &= ~WHDR_INQUEUE;
1701 first->dwFlags |= WHDR_DONE;
1703 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1705 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1709 return MMSYSERR_NOERROR;
1712 static MMRESULT WINMM_FramesToMMTime(MMTIME *time, UINT32 played_frames,
1713 UINT32 sample_rate, UINT32 bytes_per_frame)
1715 switch(time->wType){
1717 time->u.sample = played_frames;
1718 return MMSYSERR_NOERROR;
1720 time->u.ms = (DWORD)((played_frames / (double)sample_rate) * 1000);
1721 return MMSYSERR_NOERROR;
1723 time->u.smpte.fps = 30;
1724 if(played_frames >= sample_rate){
1725 time->u.smpte.sec = played_frames / (double)sample_rate;
1726 time->u.smpte.min = time->u.smpte.sec / 60;
1727 time->u.smpte.hour = time->u.smpte.min / 60;
1728 time->u.smpte.sec %= 60;
1729 time->u.smpte.min %= 60;
1730 played_frames %= sample_rate;
1732 time->u.smpte.sec = 0;
1733 time->u.smpte.min = 0;
1734 time->u.smpte.hour = 0;
1736 time->u.smpte.frame = (played_frames / (double)sample_rate) * 30;
1737 return MMSYSERR_NOERROR;
1740 time->wType = TIME_BYTES;
1741 time->u.cb = played_frames * bytes_per_frame;
1742 return MMSYSERR_NOERROR;
1745 return MMSYSERR_ERROR;
1748 static LRESULT WINMM_GetPosition(HWAVE hwave, MMTIME *time)
1750 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1751 UINT32 played_frames, sample_rate, bytes_per_frame;
1753 TRACE("(%p, %p)\n", hwave, time);
1755 if(!WINMM_ValidateAndLock(device))
1756 return MMSYSERR_INVALHANDLE;
1758 played_frames = device->played_frames;
1759 sample_rate = device->samples_per_sec;
1760 bytes_per_frame = device->bytes_per_frame;
1762 LeaveCriticalSection(&device->lock);
1764 return WINMM_FramesToMMTime(time, played_frames, sample_rate,
1768 static WINMM_MMDevice *WINMM_GetMixerMMDevice(HMIXEROBJ hmix, DWORD flags,
1771 UINT mmdev, dev, junk, *out;
1779 switch(flags & 0xF0000000){
1780 case MIXER_OBJECTF_MIXER: /* == 0 */
1781 *out = HandleToULong(hmix);
1782 if(*out < g_outmmdevices_count)
1783 return &g_out_mmdevices[*out];
1784 if(*out - g_outmmdevices_count < g_inmmdevices_count){
1785 *out -= g_outmmdevices_count;
1786 return &g_in_mmdevices[*out];
1788 /* fall through -- if it's not a valid mixer device, then
1789 * it could be a valid mixer handle. windows seems to do
1791 case MIXER_OBJECTF_HMIXER:
1792 case MIXER_OBJECTF_HWAVEOUT:
1793 case MIXER_OBJECTF_HWAVEIN:
1794 WINMM_DecomposeHWAVE((HWAVE)hmix, out, &is_out, &dev, &junk);
1795 if(junk != 0x1 || (is_out && *out >= g_outmmdevices_count) ||
1796 (!is_out && *out >= g_inmmdevices_count))
1799 return &g_out_mmdevices[*out];
1800 return &g_in_mmdevices[*out];
1801 case MIXER_OBJECTF_WAVEOUT:
1802 *out = HandleToULong(hmix);
1803 if(*out < g_outmmdevices_count)
1804 return &g_out_mmdevices[*out];
1806 case MIXER_OBJECTF_WAVEIN:
1807 *out = HandleToULong(hmix);
1808 if(*out < g_inmmdevices_count)
1809 return &g_in_mmdevices[*out];
1816 static MMRESULT WINMM_SetupMMDeviceVolume(WINMM_MMDevice *mmdevice)
1818 IAudioSessionManager *sesman;
1822 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id, &device);
1824 ERR("Device %s (%s) unavailable: %08x\n",
1825 wine_dbgstr_w(mmdevice->dev_id),
1826 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
1827 return MMSYSERR_ERROR;
1830 hr = IMMDevice_Activate(device, &IID_IAudioSessionManager,
1831 CLSCTX_INPROC_SERVER, NULL, (void**)&sesman);
1833 ERR("Activate failed: %08x\n", hr);
1834 IMMDevice_Release(device);
1835 return MMSYSERR_ERROR;
1838 IMMDevice_Release(device);
1840 hr = IAudioSessionManager_GetSimpleAudioVolume(sesman, &mmdevice->session,
1841 FALSE, &mmdevice->volume);
1842 IAudioSessionManager_Release(sesman);
1844 ERR("GetSimpleAudioVolume failed: %08x\n", hr);
1845 return MMSYSERR_ERROR;
1848 return MMSYSERR_NOERROR;
1851 static LRESULT MXD_GetControlDetails(WINMM_ControlDetails *details)
1853 WINMM_MMDevice *mmdevice;
1854 MIXERCONTROLDETAILS *control = details->details;
1857 TRACE("(%p)\n", details->hmix);
1859 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
1861 return MMSYSERR_INVALHANDLE;
1863 EnterCriticalSection(&mmdevice->lock);
1865 if(!mmdevice->volume){
1868 mr = WINMM_SetupMMDeviceVolume(mmdevice);
1869 if(mr != MMSYSERR_NOERROR){
1870 LeaveCriticalSection(&mmdevice->lock);
1875 if(control->dwControlID == 0){
1877 MIXERCONTROLDETAILS_UNSIGNED *udet;
1879 if(!control->paDetails ||
1880 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
1881 LeaveCriticalSection(&mmdevice->lock);
1882 return MMSYSERR_INVALPARAM;
1885 hr = ISimpleAudioVolume_GetMasterVolume(mmdevice->volume, &vol);
1887 ERR("GetMasterVolume failed: %08x\n", hr);
1888 LeaveCriticalSection(&mmdevice->lock);
1889 return MMSYSERR_ERROR;
1892 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
1893 udet->dwValue = vol * ((unsigned int)0xFFFF);
1894 }else if(control->dwControlID == 1){
1896 MIXERCONTROLDETAILS_BOOLEAN *bdet;
1898 if(!control->paDetails ||
1899 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
1900 LeaveCriticalSection(&mmdevice->lock);
1901 return MMSYSERR_INVALPARAM;
1904 hr = ISimpleAudioVolume_GetMute(mmdevice->volume, &mute);
1906 ERR("GetMute failed: %08x\n", hr);
1907 LeaveCriticalSection(&mmdevice->lock);
1908 return MMSYSERR_ERROR;
1911 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
1912 bdet->fValue = mute;
1913 }else if(control->dwControlID == 2 || control->dwControlID == 3){
1914 FIXME("What should the sw-side mixer controls map to?\n");
1916 LeaveCriticalSection(&mmdevice->lock);
1917 return MIXERR_INVALCONTROL;
1920 LeaveCriticalSection(&mmdevice->lock);
1922 return MMSYSERR_NOERROR;
1925 static LRESULT MXD_SetControlDetails(WINMM_ControlDetails *details)
1927 WINMM_MMDevice *mmdevice;
1928 MIXERCONTROLDETAILS *control = details->details;
1931 TRACE("(%p)\n", details->hmix);
1933 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
1935 return MMSYSERR_INVALHANDLE;
1937 EnterCriticalSection(&mmdevice->lock);
1939 if(!mmdevice->volume){
1942 mr = WINMM_SetupMMDeviceVolume(mmdevice);
1943 if(mr != MMSYSERR_NOERROR){
1944 LeaveCriticalSection(&mmdevice->lock);
1949 if(control->dwControlID == 0){
1951 MIXERCONTROLDETAILS_UNSIGNED *udet;
1953 if(!control->paDetails ||
1954 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
1955 LeaveCriticalSection(&mmdevice->lock);
1956 return MMSYSERR_INVALPARAM;
1959 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
1961 if(udet->dwValue > 65535){
1962 LeaveCriticalSection(&mmdevice->lock);
1963 return MMSYSERR_INVALPARAM;
1966 vol = udet->dwValue / 65535.f;
1968 hr = ISimpleAudioVolume_SetMasterVolume(mmdevice->volume, vol, NULL);
1970 ERR("SetMasterVolume failed: %08x\n", hr);
1971 LeaveCriticalSection(&mmdevice->lock);
1972 return MMSYSERR_ERROR;
1974 }else if(control->dwControlID == 1){
1976 MIXERCONTROLDETAILS_BOOLEAN *bdet;
1978 if(!control->paDetails ||
1979 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
1980 LeaveCriticalSection(&mmdevice->lock);
1981 return MMSYSERR_INVALPARAM;
1984 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
1985 mute = bdet->fValue;
1987 hr = ISimpleAudioVolume_SetMute(mmdevice->volume, mute, NULL);
1989 ERR("SetMute failed: %08x\n", hr);
1990 LeaveCriticalSection(&mmdevice->lock);
1991 return MMSYSERR_ERROR;
1993 }else if(control->dwControlID == 2 || control->dwControlID == 3){
1994 FIXME("What should the sw-side mixer controls map to?\n");
1996 LeaveCriticalSection(&mmdevice->lock);
1997 return MIXERR_INVALCONTROL;
2000 LeaveCriticalSection(&mmdevice->lock);
2002 return MMSYSERR_NOERROR;
2005 static LRESULT CALLBACK WINMM_DevicesMsgProc(HWND hwnd, UINT msg, WPARAM wparam,
2010 return WOD_Open((WINMM_OpenInfo*)wparam);
2012 return WOD_Close((HWAVEOUT)wparam);
2014 return WID_Open((WINMM_OpenInfo*)wparam);
2016 return WID_Close((HWAVEIN)wparam);
2017 case MXDM_GETCONTROLDETAILS:
2018 return MXD_GetControlDetails((WINMM_ControlDetails*)wparam);
2019 case MXDM_SETCONTROLDETAILS:
2020 return MXD_SetControlDetails((WINMM_ControlDetails*)wparam);
2022 return DefWindowProcW(hwnd, msg, wparam, lparam);
2025 static DWORD WINAPI WINMM_DevicesThreadProc(void *arg)
2029 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
2031 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2033 ERR("CoInitializeEx failed: %08x\n", hr);
2037 hr = WINMM_InitMMDevices();
2043 g_devices_hwnd = CreateWindowW(messageW, NULL, 0, 0, 0, 0, 0,
2044 HWND_MESSAGE, NULL, NULL, NULL);
2045 if(!g_devices_hwnd){
2046 ERR("CreateWindow failed: %d\n", GetLastError());
2051 SetWindowLongPtrW(g_devices_hwnd, GWLP_WNDPROC,
2052 (LONG_PTR)WINMM_DevicesMsgProc);
2054 /* inform caller that the thread is ready to process messages */
2056 evt = NULL; /* do not use after this point */
2060 wait = MsgWaitForMultipleObjects(g_devhandle_count, g_device_handles,
2061 FALSE, INFINITE, QS_ALLINPUT);
2062 if(wait == g_devhandle_count + WAIT_OBJECT_0){
2064 if(PeekMessageW(&msg, g_devices_hwnd, 0, 0, PM_REMOVE))
2065 ERR("Unexpected message: 0x%x\n", msg.message);
2066 }else if(wait < g_devhandle_count + WAIT_OBJECT_0){
2067 WINMM_Device *device = g_handle_devices[wait - WAIT_OBJECT_0];
2069 WOD_PushData(device);
2071 WID_PullData(device);
2073 ERR("Unexpected MsgWait result 0x%x, GLE: %d\n", wait,
2077 DestroyWindow(g_devices_hwnd);
2084 static BOOL WINMM_StartDevicesThread(void)
2089 EnterCriticalSection(&g_devthread_lock);
2091 if(g_devices_thread){
2094 wait = WaitForSingleObject(g_devices_thread, 0);
2095 if(wait == WAIT_TIMEOUT){
2096 LeaveCriticalSection(&g_devthread_lock);
2099 if(wait != WAIT_OBJECT_0){
2100 LeaveCriticalSection(&g_devthread_lock);
2104 g_devices_thread = NULL;
2105 g_devices_hwnd = NULL;
2108 TRACE("Starting up devices thread\n");
2110 events[0] = CreateEventW(NULL, FALSE, FALSE, NULL);
2112 g_devices_thread = CreateThread(NULL, 0, WINMM_DevicesThreadProc,
2113 events[0], 0, NULL);
2114 if(!g_devices_thread){
2115 LeaveCriticalSection(&g_devthread_lock);
2116 CloseHandle(events[0]);
2120 events[1] = g_devices_thread;
2121 wait = WaitForMultipleObjects(2, events, FALSE, INFINITE);
2122 CloseHandle(events[0]);
2123 if(wait != WAIT_OBJECT_0){
2124 if(wait == 1 + WAIT_OBJECT_0){
2125 CloseHandle(g_devices_thread);
2126 g_devices_thread = NULL;
2127 g_devices_hwnd = NULL;
2129 LeaveCriticalSection(&g_devthread_lock);
2133 LeaveCriticalSection(&g_devthread_lock);
2138 /**************************************************************************
2139 * waveOutGetNumDevs [WINMM.@]
2141 UINT WINAPI waveOutGetNumDevs(void)
2143 if(!WINMM_StartDevicesThread())
2146 TRACE("count: %u\n", g_outmmdevices_count);
2148 return g_outmmdevices_count;
2151 /**************************************************************************
2152 * waveOutGetDevCapsA [WINMM.@]
2154 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2160 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2162 if(!WINMM_StartDevicesThread())
2163 return MMSYSERR_ERROR;
2166 return MMSYSERR_INVALPARAM;
2168 ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2170 if (ret == MMSYSERR_NOERROR) {
2172 wocA.wMid = wocW.wMid;
2173 wocA.wPid = wocW.wPid;
2174 wocA.vDriverVersion = wocW.vDriverVersion;
2175 WideCharToMultiByte( CP_ACP, 0, wocW.szPname, -1, wocA.szPname,
2176 sizeof(wocA.szPname), NULL, NULL );
2177 wocA.dwFormats = wocW.dwFormats;
2178 wocA.wChannels = wocW.wChannels;
2179 wocA.dwSupport = wocW.dwSupport;
2180 memcpy(lpCaps, &wocA, min(uSize, sizeof(wocA)));
2185 /**************************************************************************
2186 * waveOutGetDevCapsW [WINMM.@]
2188 UINT WINAPI waveOutGetDevCapsW(UINT_PTR uDeviceID, LPWAVEOUTCAPSW lpCaps,
2191 WAVEOUTCAPSW mapper_caps, *caps;
2193 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2195 if(!WINMM_StartDevicesThread())
2196 return MMSYSERR_ERROR;
2198 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2200 if(WINMM_IsMapper(uDeviceID)){
2201 /* FIXME: Should be localized */
2202 static const WCHAR mapper_pnameW[] = {'W','i','n','e',' ','S','o','u',
2203 'n','d',' ','M','a','p','p','e','r',0};
2205 mapper_caps.wMid = 0xFF;
2206 mapper_caps.wPid = 0xFF;
2207 mapper_caps.vDriverVersion = 0x00010001;
2208 mapper_caps.dwFormats = 0xFFFFFFFF;
2209 mapper_caps.wReserved1 = 0;
2210 mapper_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
2211 WAVECAPS_SAMPLEACCURATE;
2212 mapper_caps.wChannels = 2;
2213 lstrcpyW(mapper_caps.szPname, mapper_pnameW);
2215 caps = &mapper_caps;
2217 if(uDeviceID >= g_outmmdevices_count)
2218 return MMSYSERR_BADDEVICEID;
2220 caps = &g_out_mmdevices[uDeviceID].out_caps;
2223 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2225 return MMSYSERR_NOERROR;
2228 /**************************************************************************
2229 * waveOutGetErrorTextA [WINMM.@]
2230 * waveInGetErrorTextA [WINMM.@]
2232 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2236 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2237 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2240 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2241 if (!xstr) ret = MMSYSERR_NOMEM;
2244 ret = waveOutGetErrorTextW(uError, xstr, uSize);
2245 if (ret == MMSYSERR_NOERROR)
2246 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
2247 HeapFree(GetProcessHeap(), 0, xstr);
2253 /**************************************************************************
2254 * waveOutGetErrorTextW [WINMM.@]
2255 * waveInGetErrorTextW [WINMM.@]
2257 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2259 UINT ret = MMSYSERR_BADERRNUM;
2261 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2262 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2264 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2265 * a warning for the test was always true */
2266 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
2267 (uError >= WAVERR_BASE && uError <= WAVERR_LASTERROR)) {
2268 if (LoadStringW(hWinMM32Instance,
2269 uError, lpText, uSize) > 0) {
2270 ret = MMSYSERR_NOERROR;
2276 /**************************************************************************
2277 * waveOutOpen [WINMM.@]
2278 * All the args/structs have the same layout as the win16 equivalents
2280 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2281 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2282 DWORD_PTR dwInstance, DWORD dwFlags)
2285 WINMM_OpenInfo info;
2286 WINMM_CBInfo cb_info;
2288 TRACE("(%p, %u, %p, %lx, %lx, %08x)\n", lphWaveOut, uDeviceID, lpFormat,
2289 dwCallback, dwInstance, dwFlags);
2291 if(!WINMM_StartDevicesThread())
2292 return MMSYSERR_ERROR;
2294 if(!lphWaveOut && !(dwFlags & WAVE_FORMAT_QUERY))
2295 return MMSYSERR_INVALPARAM;
2297 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
2298 if(res != MMSYSERR_NOERROR)
2301 info.format = (WAVEFORMATEX*)lpFormat;
2302 info.callback = dwCallback;
2303 info.cb_user = dwInstance;
2304 info.req_device = uDeviceID;
2305 info.flags = dwFlags;
2307 res = SendMessageW(g_devices_hwnd, WODM_OPEN, (DWORD_PTR)&info, 0);
2308 if(res != MMSYSERR_NOERROR)
2312 *lphWaveOut = (HWAVEOUT)info.handle;
2314 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2315 cb_info.callback = dwCallback;
2316 cb_info.user = dwInstance;
2317 cb_info.hwave = info.handle;
2319 WINMM_NotifyClient(&cb_info, WOM_OPEN, 0, 0);
2324 /**************************************************************************
2325 * waveOutClose [WINMM.@]
2327 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2330 WINMM_Device *device;
2331 WINMM_CBInfo cb_info;
2333 TRACE("(%p)\n", hWaveOut);
2335 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2337 if(!WINMM_ValidateAndLock(device))
2338 return MMSYSERR_INVALHANDLE;
2340 cb_info = device->cb_info;
2342 LeaveCriticalSection(&device->lock);
2344 res = SendMessageW(g_devices_hwnd, WODM_CLOSE, (WPARAM)hWaveOut, 0);
2346 if(res == MMSYSERR_NOERROR)
2347 WINMM_NotifyClient(&cb_info, WOM_CLOSE, 0, 0);
2352 /**************************************************************************
2353 * waveOutPrepareHeader [WINMM.@]
2355 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2356 WAVEHDR* lpWaveOutHdr, UINT uSize)
2358 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2360 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2361 return MMSYSERR_INVALPARAM;
2363 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2364 return WAVERR_STILLPLAYING;
2366 return WINMM_PrepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2369 /**************************************************************************
2370 * waveOutUnprepareHeader [WINMM.@]
2372 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2373 LPWAVEHDR lpWaveOutHdr, UINT uSize)
2375 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2377 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2378 return MMSYSERR_INVALPARAM;
2380 if(!(lpWaveOutHdr->dwFlags & WHDR_PREPARED))
2381 return MMSYSERR_NOERROR;
2383 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2384 return WAVERR_STILLPLAYING;
2386 return WINMM_UnprepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2389 /**************************************************************************
2390 * waveOutWrite [WINMM.@]
2392 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, WAVEHDR *header, UINT uSize)
2394 WINMM_Device *device;
2397 TRACE("(%p, %p, %u)\n", hWaveOut, header, uSize);
2399 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2401 if(!WINMM_ValidateAndLock(device))
2402 return MMSYSERR_INVALHANDLE;
2404 if(!header->lpData || !(header->dwFlags & WHDR_PREPARED)){
2405 LeaveCriticalSection(&device->lock);
2406 return WAVERR_UNPREPARED;
2409 if(header->dwFlags & WHDR_INQUEUE){
2410 LeaveCriticalSection(&device->lock);
2411 return WAVERR_STILLPLAYING;
2414 if(device->acm_handle){
2415 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
2418 ash->cbSrcLength = header->dwBufferLength;
2419 mr = acmStreamConvert(device->acm_handle, ash, 0);
2420 if(mr != MMSYSERR_NOERROR){
2421 LeaveCriticalSection(&device->lock);
2427 device->last->lpNext = header;
2428 device->last = header;
2429 if(!device->playing)
2430 device->playing = header;
2432 device->playing = device->first = device->last = header;
2433 if(header->dwFlags & WHDR_BEGINLOOP){
2434 device->loop_counter = header->dwLoops;
2435 device->loop_start = header;
2439 header->lpNext = NULL;
2440 header->dwFlags &= ~WHDR_DONE;
2441 header->dwFlags |= WHDR_INQUEUE;
2443 hr = WINMM_BeginPlaying(device);
2445 LeaveCriticalSection(&device->lock);
2446 return MMSYSERR_ERROR;
2449 LeaveCriticalSection(&device->lock);
2451 return MMSYSERR_NOERROR;
2454 /**************************************************************************
2455 * waveOutBreakLoop [WINMM.@]
2457 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2459 WINMM_Device *device;
2461 TRACE("(%p)\n", hWaveOut);
2463 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2465 if(!WINMM_ValidateAndLock(device))
2466 return MMSYSERR_INVALHANDLE;
2468 device->loop_counter = 0;
2470 LeaveCriticalSection(&device->lock);
2472 return MMSYSERR_NOERROR;
2475 /**************************************************************************
2476 * waveOutPause [WINMM.@]
2478 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2480 TRACE("(%p)\n", hWaveOut);
2482 return WINMM_Pause((HWAVE)hWaveOut);
2485 /**************************************************************************
2486 * waveOutReset [WINMM.@]
2488 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2490 TRACE("(%p)\n", hWaveOut);
2492 return WINMM_Reset((HWAVE)hWaveOut);
2495 /**************************************************************************
2496 * waveOutRestart [WINMM.@]
2498 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2500 WINMM_Device *device;
2503 TRACE("(%p)\n", hWaveOut);
2505 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2507 if(!WINMM_ValidateAndLock(device))
2508 return MMSYSERR_INVALHANDLE;
2510 device->stopped = TRUE;
2512 hr = WINMM_BeginPlaying(device);
2514 LeaveCriticalSection(&device->lock);
2515 return MMSYSERR_ERROR;
2518 LeaveCriticalSection(&device->lock);
2520 return MMSYSERR_NOERROR;
2523 /**************************************************************************
2524 * waveOutGetPosition [WINMM.@]
2526 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2529 TRACE("(%p, %p, %u)\n", hWaveOut, lpTime, uSize);
2531 if(!uSize || !lpTime || uSize != sizeof(MMTIME))
2532 return MMSYSERR_INVALPARAM;
2534 return WINMM_GetPosition((HWAVE)hWaveOut, lpTime);
2537 /**************************************************************************
2538 * waveOutGetPitch [WINMM.@]
2540 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
2542 TRACE("(%p, %p)\n", hWaveOut, lpdw);
2543 return MMSYSERR_NOTSUPPORTED;
2546 /**************************************************************************
2547 * waveOutSetPitch [WINMM.@]
2549 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
2551 TRACE("(%p, %08x)\n", hWaveOut, dw);
2553 return MMSYSERR_NOTSUPPORTED;
2556 /**************************************************************************
2557 * waveOutGetPlaybackRate [WINMM.@]
2559 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
2561 TRACE("(%p, %p)\n", hWaveOut, lpdw);
2563 return MMSYSERR_NOTSUPPORTED;
2566 /**************************************************************************
2567 * waveOutSetPlaybackRate [WINMM.@]
2569 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
2571 TRACE("(%p, %08x)\n", hWaveOut, dw);
2573 return MMSYSERR_NOTSUPPORTED;
2576 /**************************************************************************
2577 * waveOutGetVolume [WINMM.@]
2579 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, DWORD *out)
2581 WINMM_Device *device;
2586 TRACE("(%p, %p)\n", hWaveOut, out);
2589 return MMSYSERR_INVALPARAM;
2591 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2593 if(!WINMM_ValidateAndLock(device))
2594 return MMSYSERR_INVALHANDLE;
2596 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
2598 LeaveCriticalSection(&device->lock);
2599 ERR("GetChannelCount failed: %08x\n", hr);
2600 return MMSYSERR_ERROR;
2603 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
2605 LeaveCriticalSection(&device->lock);
2606 return MMSYSERR_NOMEM;
2609 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
2611 LeaveCriticalSection(&device->lock);
2612 HeapFree(GetProcessHeap(), 0, vols);
2613 ERR("GetAllVolumes failed: %08x\n", hr);
2614 return MMSYSERR_ERROR;
2617 LeaveCriticalSection(&device->lock);
2619 *out = ((UINT16)(vols[0] * (DWORD)0xFFFF));
2621 *out |= ((UINT16)(vols[1] * (DWORD)0xFFFF)) << 16;
2623 HeapFree(GetProcessHeap(), 0, vols);
2625 return MMSYSERR_NOERROR;
2628 /**************************************************************************
2629 * waveOutSetVolume [WINMM.@]
2631 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD in)
2633 WINMM_Device *device;
2638 TRACE("(%p, %08x)\n", hWaveOut, in);
2640 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2642 if(!WINMM_ValidateAndLock(device))
2643 return MMSYSERR_INVALHANDLE;
2645 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
2647 LeaveCriticalSection(&device->lock);
2648 ERR("GetChannelCount failed: %08x\n", hr);
2649 return MMSYSERR_ERROR;
2652 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
2654 LeaveCriticalSection(&device->lock);
2655 return MMSYSERR_NOMEM;
2658 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
2660 LeaveCriticalSection(&device->lock);
2661 HeapFree(GetProcessHeap(), 0, vols);
2662 ERR("GetAllVolumes failed: %08x\n", hr);
2663 return MMSYSERR_ERROR;
2666 vols[0] = (float)((DWORD)(in & 0xFFFF) / (float)0xFFFF);
2668 vols[1] = (float)((DWORD)(in >> 16) / (float)0xFFFF);
2670 hr = IAudioStreamVolume_SetAllVolumes(device->volume, channels, vols);
2672 LeaveCriticalSection(&device->lock);
2673 HeapFree(GetProcessHeap(), 0, vols);
2674 ERR("SetAllVolumes failed: %08x\n", hr);
2675 return MMSYSERR_ERROR;
2678 LeaveCriticalSection(&device->lock);
2680 HeapFree(GetProcessHeap(), 0, vols);
2682 return MMSYSERR_NOERROR;
2685 /**************************************************************************
2686 * waveOutGetID [WINMM.@]
2688 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
2690 WINMM_Device *device;
2694 TRACE("(%p, %p)\n", hWaveOut, lpuDeviceID);
2697 return MMSYSERR_INVALPARAM;
2699 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2700 if(!WINMM_ValidateAndLock(device))
2701 return MMSYSERR_INVALHANDLE;
2703 LeaveCriticalSection(&device->lock);
2705 WINMM_DecomposeHWAVE((HWAVE)hWaveOut, lpuDeviceID, &is_out, &dev, &junk);
2707 return MMSYSERR_NOERROR;
2710 static UINT WINMM_QueryInstanceIDSize(UINT device, DWORD_PTR *len, BOOL is_out)
2713 WINMM_MMDevice *devices;
2715 TRACE("(%u, %p, %d)\n", device, len, is_out);
2718 count = g_outmmdevices_count;
2719 devices = g_out_mmdevices;
2721 count = g_inmmdevices_count;
2722 devices = g_in_mmdevices;
2726 return MMSYSERR_INVALHANDLE;
2728 *len = (lstrlenW(devices[device].dev_id) + 1) * sizeof(WCHAR);
2730 return MMSYSERR_NOERROR;
2733 static UINT WINMM_QueryInstanceID(UINT device, WCHAR *str, DWORD_PTR len,
2737 WINMM_MMDevice *devices;
2739 TRACE("(%u, %p, %d)\n", device, str, is_out);
2742 count = g_outmmdevices_count;
2743 devices = g_out_mmdevices;
2745 count = g_inmmdevices_count;
2746 devices = g_in_mmdevices;
2750 return MMSYSERR_INVALHANDLE;
2752 id_len = (lstrlenW(devices[device].dev_id) + 1) * sizeof(WCHAR);
2754 return MMSYSERR_ERROR;
2756 memcpy(str, devices[device].dev_id, id_len);
2758 return MMSYSERR_NOERROR;
2761 /**************************************************************************
2762 * waveOutMessage [WINMM.@]
2764 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
2765 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2767 TRACE("(%p, %u, %lx, %lx)\n", hWaveOut, uMessage, dwParam1, dwParam2);
2770 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
2771 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut),
2772 (DWORD_PTR*)dwParam1, TRUE);
2773 case DRV_QUERYFUNCTIONINSTANCEID:
2774 return WINMM_QueryInstanceID(HandleToULong(hWaveOut), (WCHAR*)dwParam1, dwParam2, TRUE);
2777 return MMSYSERR_NOTSUPPORTED;
2780 /**************************************************************************
2781 * waveInGetNumDevs [WINMM.@]
2783 UINT WINAPI waveInGetNumDevs(void)
2785 if(!WINMM_StartDevicesThread())
2788 TRACE("count: %u\n", g_inmmdevices_count);
2790 return g_inmmdevices_count;
2793 /**************************************************************************
2794 * waveInGetDevCapsW [WINMM.@]
2796 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
2798 WAVEINCAPSW mapper_caps, *caps;
2800 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2802 if(!WINMM_StartDevicesThread())
2803 return MMSYSERR_ERROR;
2806 return MMSYSERR_INVALPARAM;
2808 if(WINMM_IsMapper(uDeviceID)){
2809 /* FIXME: Should be localized */
2810 static const WCHAR mapper_pnameW[] = {'W','i','n','e',' ','S','o','u',
2811 'n','d',' ','M','a','p','p','e','r',0};
2813 mapper_caps.wMid = 0xFF;
2814 mapper_caps.wPid = 0xFF;
2815 mapper_caps.vDriverVersion = 0x00010001;
2816 mapper_caps.dwFormats = 0xFFFFFFFF;
2817 mapper_caps.wReserved1 = 0;
2818 mapper_caps.wChannels = 2;
2819 lstrcpyW(mapper_caps.szPname, mapper_pnameW);
2821 caps = &mapper_caps;
2823 if(uDeviceID >= g_inmmdevices_count)
2824 return MMSYSERR_BADDEVICEID;
2826 caps = &g_in_mmdevices[uDeviceID].in_caps;
2829 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2831 return MMSYSERR_NOERROR;
2834 /**************************************************************************
2835 * waveInGetDevCapsA [WINMM.@]
2837 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
2842 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2844 if(!WINMM_StartDevicesThread())
2845 return MMSYSERR_ERROR;
2848 return MMSYSERR_INVALPARAM;
2850 ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
2852 if (ret == MMSYSERR_NOERROR) {
2854 wicA.wMid = wicW.wMid;
2855 wicA.wPid = wicW.wPid;
2856 wicA.vDriverVersion = wicW.vDriverVersion;
2857 WideCharToMultiByte( CP_ACP, 0, wicW.szPname, -1, wicA.szPname,
2858 sizeof(wicA.szPname), NULL, NULL );
2859 wicA.dwFormats = wicW.dwFormats;
2860 wicA.wChannels = wicW.wChannels;
2861 memcpy(lpCaps, &wicA, min(uSize, sizeof(wicA)));
2866 /**************************************************************************
2867 * waveInOpen [WINMM.@]
2869 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
2870 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2871 DWORD_PTR dwInstance, DWORD dwFlags)
2874 WINMM_OpenInfo info;
2875 WINMM_CBInfo cb_info;
2877 TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn, uDeviceID, lpFormat,
2878 dwCallback, dwInstance, dwFlags);
2880 if(!WINMM_StartDevicesThread())
2881 return MMSYSERR_ERROR;
2883 if(!lphWaveIn && !(dwFlags & WAVE_FORMAT_QUERY))
2884 return MMSYSERR_INVALPARAM;
2886 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
2887 if(res != MMSYSERR_NOERROR)
2890 info.format = (WAVEFORMATEX*)lpFormat;
2891 info.callback = dwCallback;
2892 info.cb_user = dwInstance;
2893 info.req_device = uDeviceID;
2894 info.flags = dwFlags;
2896 res = SendMessageW(g_devices_hwnd, WIDM_OPEN, (DWORD_PTR)&info, 0);
2897 if(res != MMSYSERR_NOERROR)
2901 *lphWaveIn = (HWAVEIN)info.handle;
2903 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2904 cb_info.callback = dwCallback;
2905 cb_info.user = dwInstance;
2906 cb_info.hwave = info.handle;
2908 WINMM_NotifyClient(&cb_info, WIM_OPEN, 0, 0);
2913 /**************************************************************************
2914 * waveInClose [WINMM.@]
2916 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
2918 WINMM_Device *device;
2919 WINMM_CBInfo cb_info;
2922 TRACE("(%p)\n", hWaveIn);
2924 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
2926 if(!WINMM_ValidateAndLock(device))
2927 return MMSYSERR_INVALHANDLE;
2929 cb_info = device->cb_info;
2931 LeaveCriticalSection(&device->lock);
2933 res = SendMessageW(g_devices_hwnd, WIDM_CLOSE, (WPARAM)hWaveIn, 0);
2935 if(res == MMSYSERR_NOERROR)
2936 WINMM_NotifyClient(&cb_info, WIM_CLOSE, 0, 0);
2941 /**************************************************************************
2942 * waveInPrepareHeader [WINMM.@]
2944 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
2947 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
2949 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
2950 return MMSYSERR_INVALPARAM;
2952 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
2953 return WAVERR_STILLPLAYING;
2955 return WINMM_PrepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
2958 /**************************************************************************
2959 * waveInUnprepareHeader [WINMM.@]
2961 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
2964 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
2966 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
2967 return MMSYSERR_INVALPARAM;
2969 if(!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
2970 return MMSYSERR_NOERROR;
2972 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
2973 return WAVERR_STILLPLAYING;
2975 return WINMM_UnprepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
2978 /**************************************************************************
2979 * waveInAddBuffer [WINMM.@]
2981 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn, WAVEHDR *header, UINT uSize)
2983 WINMM_Device *device;
2985 TRACE("(%p, %p, %u)\n", hWaveIn, header, uSize);
2987 if(!header || uSize < sizeof(WAVEHDR))
2988 return MMSYSERR_INVALPARAM;
2990 if(!(header->dwFlags & WHDR_PREPARED))
2991 return WAVERR_UNPREPARED;
2993 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
2995 if(!WINMM_ValidateAndLock(device))
2996 return MMSYSERR_INVALHANDLE;
2999 device->first = device->last = header;
3001 device->last->lpNext = header;
3002 device->last = header;
3005 header->dwBytesRecorded = 0;
3006 header->lpNext = NULL;
3007 header->dwFlags &= ~WHDR_DONE;
3008 header->dwFlags |= WHDR_INQUEUE;
3010 LeaveCriticalSection(&device->lock);
3012 return MMSYSERR_NOERROR;
3015 /**************************************************************************
3016 * waveInReset [WINMM.@]
3018 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
3020 TRACE("(%p)\n", hWaveIn);
3022 return WINMM_Reset((HWAVE)hWaveIn);
3025 /**************************************************************************
3026 * waveInStart [WINMM.@]
3028 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
3030 WINMM_Device *device;
3033 TRACE("(%p)\n", hWaveIn);
3035 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3037 if(!WINMM_ValidateAndLock(device))
3038 return MMSYSERR_INVALHANDLE;
3040 hr = WINMM_BeginPlaying(device);
3042 LeaveCriticalSection(&device->lock);
3043 return MMSYSERR_ERROR;
3046 LeaveCriticalSection(&device->lock);
3048 return MMSYSERR_NOERROR;
3051 /**************************************************************************
3052 * waveInStop [WINMM.@]
3054 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
3056 WINMM_CBInfo cb_info;
3057 WINMM_Device *device;
3061 TRACE("(%p)\n", hWaveIn);
3063 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3065 if(!WINMM_ValidateAndLock(device))
3066 return MMSYSERR_INVALHANDLE;
3068 hr = WINMM_Pause((HWAVE)hWaveIn);
3070 LeaveCriticalSection(&device->lock);
3071 return MMSYSERR_ERROR;
3073 device->stopped = TRUE;
3075 buf = device->first;
3076 if(buf && buf->dwBytesRecorded > 0){
3077 device->first = buf->lpNext;
3078 buf->dwFlags &= ~WHDR_INQUEUE;
3079 buf->dwFlags |= WHDR_DONE;
3083 cb_info = device->cb_info;
3085 LeaveCriticalSection(&device->lock);
3088 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)buf, 0);
3090 return MMSYSERR_NOERROR;
3093 /**************************************************************************
3094 * waveInGetPosition [WINMM.@]
3096 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
3099 TRACE("(%p, %p, %u)\n", hWaveIn, lpTime, uSize);
3101 if(!uSize || !lpTime || uSize != sizeof(MMTIME))
3102 return MMSYSERR_INVALPARAM;
3104 return WINMM_GetPosition((HWAVE)hWaveIn, lpTime);
3107 /**************************************************************************
3108 * waveInGetID [WINMM.@]
3110 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
3114 WINMM_Device *device;
3116 TRACE("(%p, %p)\n", hWaveIn, lpuDeviceID);
3119 return MMSYSERR_INVALPARAM;
3121 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3122 if(!WINMM_ValidateAndLock(device))
3123 return MMSYSERR_INVALHANDLE;
3125 LeaveCriticalSection(&device->lock);
3127 WINMM_DecomposeHWAVE((HWAVE)hWaveIn, lpuDeviceID, &is_out, &dev, &junk);
3129 return MMSYSERR_NOERROR;
3132 /**************************************************************************
3133 * waveInMessage [WINMM.@]
3135 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
3136 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3138 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
3141 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3142 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn),
3143 (DWORD_PTR*)dwParam1, FALSE);
3144 case DRV_QUERYFUNCTIONINSTANCEID:
3145 return WINMM_QueryInstanceID(HandleToULong(hWaveIn), (WCHAR*)dwParam1, dwParam2, FALSE);
3148 return MMSYSERR_NOTSUPPORTED;
3151 UINT WINAPI mixerGetNumDevs(void)
3155 if(!WINMM_StartDevicesThread())
3158 return g_outmmdevices_count + g_inmmdevices_count;
3161 /**************************************************************************
3162 * mixerGetDevCapsA [WINMM.@]
3164 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
3169 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3172 return MMSYSERR_INVALPARAM;
3174 ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
3176 if (ret == MMSYSERR_NOERROR) {
3178 micA.wMid = micW.wMid;
3179 micA.wPid = micW.wPid;
3180 micA.vDriverVersion = micW.vDriverVersion;
3181 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
3182 sizeof(micA.szPname), NULL, NULL );
3183 micA.fdwSupport = micW.fdwSupport;
3184 micA.cDestinations = micW.cDestinations;
3185 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
3190 /**************************************************************************
3191 * mixerGetDevCapsW [WINMM.@]
3193 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
3195 WINMM_MMDevice *mmdevice;
3198 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3200 if(!WINMM_StartDevicesThread())
3201 return MMSYSERR_ERROR;
3204 return MMSYSERR_INVALPARAM;
3207 return MMSYSERR_NOERROR;
3209 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3210 return MMSYSERR_BADDEVICEID;
3212 if(uDeviceID < g_outmmdevices_count){
3213 mmdevice = &g_out_mmdevices[uDeviceID];
3214 memcpy(caps.szPname, mmdevice->out_caps.szPname, sizeof(caps.szPname));
3216 mmdevice = &g_in_mmdevices[uDeviceID - g_outmmdevices_count];
3217 memcpy(caps.szPname, mmdevice->in_caps.szPname, sizeof(caps.szPname));
3222 caps.vDriverVersion = 0x00010001;
3223 caps.fdwSupport = 0;
3224 caps.cDestinations = 1;
3226 memcpy(lpCaps, &caps, uSize);
3228 return MMSYSERR_NOERROR;
3231 /**************************************************************************
3232 * mixerOpen [WINMM.@]
3234 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
3235 DWORD_PTR dwInstance, DWORD fdwOpen)
3237 WINMM_MMDevice *mmdevice;
3240 TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix, uDeviceID, dwCallback,
3241 dwInstance, fdwOpen);
3243 if(!WINMM_StartDevicesThread())
3244 return MMSYSERR_ERROR;
3247 return MMSYSERR_INVALPARAM;
3249 mr = WINMM_CheckCallback(dwCallback, fdwOpen, TRUE);
3250 if(mr != MMSYSERR_NOERROR)
3253 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3254 return MMSYSERR_BADDEVICEID;
3256 if(uDeviceID < g_outmmdevices_count){
3257 mmdevice = &g_out_mmdevices[uDeviceID];
3258 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID, TRUE,
3259 mmdevice->mixer_count);
3261 mmdevice = &g_in_mmdevices[uDeviceID - g_outmmdevices_count];
3262 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID - g_outmmdevices_count,
3263 FALSE, mmdevice->mixer_count);
3266 ++mmdevice->mixer_count;
3268 return MMSYSERR_NOERROR;
3271 /**************************************************************************
3272 * mixerClose [WINMM.@]
3274 UINT WINAPI mixerClose(HMIXER hMix)
3276 TRACE("(%p)\n", hMix);
3278 return MMSYSERR_NOERROR;
3281 /**************************************************************************
3282 * mixerGetID [WINMM.@]
3284 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
3286 WINMM_MMDevice *mmdevice;
3288 TRACE("(%p, %p, %x)\n", hmix, lpid, fdwID);
3290 if(!WINMM_StartDevicesThread())
3291 return MMSYSERR_ERROR;
3294 return MMSYSERR_INVALPARAM;
3296 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwID, lpid);
3298 return MMSYSERR_INVALHANDLE;
3300 if(mmdevice->in_caps.szPname[0] != '\0')
3301 *lpid += g_outmmdevices_count;
3303 return MMSYSERR_NOERROR;
3306 /**************************************************************************
3307 * mixerGetControlDetailsW [WINMM.@]
3309 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
3312 WINMM_ControlDetails details;
3314 TRACE("(%p, %p, %x)\n", hmix, lpmcdW, fdwDetails);
3316 if(!WINMM_StartDevicesThread())
3317 return MMSYSERR_ERROR;
3320 return MMSYSERR_INVALPARAM;
3322 TRACE("dwControlID: %u\n", lpmcdW->dwControlID);
3324 details.hmix = hmix;
3325 details.details = lpmcdW;
3326 details.flags = fdwDetails;
3328 return SendMessageW(g_devices_hwnd, MXDM_GETCONTROLDETAILS,
3329 (DWORD_PTR)&details, 0);
3332 /**************************************************************************
3333 * mixerGetControlDetailsA [WINMM.@]
3335 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
3338 UINT ret = MMSYSERR_NOTSUPPORTED;
3340 TRACE("(%p, %p, %08x)\n", hmix, lpmcdA, fdwDetails);
3342 if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
3343 return MMSYSERR_INVALPARAM;
3345 switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
3346 case MIXER_GETCONTROLDETAILSF_VALUE:
3347 /* can safely use A structure as it is, no string inside */
3348 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3350 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
3352 MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
3353 MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
3354 int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3357 if (lpmcdA->u.cMultipleItems != 0) {
3358 size *= lpmcdA->u.cMultipleItems;
3360 pDetailsW = HeapAlloc(GetProcessHeap(), 0, size);
3361 lpmcdA->paDetails = pDetailsW;
3362 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3363 /* set up lpmcd->paDetails */
3364 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3365 /* copy from lpmcd->paDetails back to paDetailsW; */
3366 if (ret == MMSYSERR_NOERROR) {
3367 for (i = 0; i < lpmcdA->u.cMultipleItems * lpmcdA->cChannels; i++) {
3368 pDetailsA->dwParam1 = pDetailsW->dwParam1;
3369 pDetailsA->dwParam2 = pDetailsW->dwParam2;
3370 WideCharToMultiByte( CP_ACP, 0, pDetailsW->szName, -1,
3372 sizeof(pDetailsA->szName), NULL, NULL );
3376 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3377 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3379 HeapFree(GetProcessHeap(), 0, pDetailsW);
3380 lpmcdA->paDetails = pDetailsA;
3381 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
3385 ERR("Unsupported fdwDetails=0x%08x\n", fdwDetails);
3391 /**************************************************************************
3392 * mixerGetLineControlsA [WINMM.@]
3394 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
3397 MIXERLINECONTROLSW mlcW;
3401 TRACE("(%p, %p, %x)\n", hmix, lpmlcA, fdwControls);
3403 if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
3404 lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
3405 return MMSYSERR_INVALPARAM;
3407 mlcW.cbStruct = sizeof(mlcW);
3408 mlcW.dwLineID = lpmlcA->dwLineID;
3409 mlcW.u.dwControlID = lpmlcA->u.dwControlID;
3410 mlcW.u.dwControlType = lpmlcA->u.dwControlType;
3412 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3413 the control count is assumed to be 1 - This is relied upon by a game,
3414 "Dynomite Deluze" */
3415 if (MIXER_GETLINECONTROLSF_ONEBYTYPE == (fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK)) {
3418 mlcW.cControls = lpmlcA->cControls;
3420 mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
3421 mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
3422 mlcW.cControls * mlcW.cbmxctrl);
3424 ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
3426 if (ret == MMSYSERR_NOERROR) {
3427 lpmlcA->dwLineID = mlcW.dwLineID;
3428 lpmlcA->u.dwControlID = mlcW.u.dwControlID;
3429 lpmlcA->u.dwControlType = mlcW.u.dwControlType;
3431 for (i = 0; i < mlcW.cControls; i++) {
3432 lpmlcA->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLA);
3433 lpmlcA->pamxctrl[i].dwControlID = mlcW.pamxctrl[i].dwControlID;
3434 lpmlcA->pamxctrl[i].dwControlType = mlcW.pamxctrl[i].dwControlType;
3435 lpmlcA->pamxctrl[i].fdwControl = mlcW.pamxctrl[i].fdwControl;
3436 lpmlcA->pamxctrl[i].cMultipleItems = mlcW.pamxctrl[i].cMultipleItems;
3437 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szShortName, -1,
3438 lpmlcA->pamxctrl[i].szShortName,
3439 sizeof(lpmlcA->pamxctrl[i].szShortName), NULL, NULL );
3440 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szName, -1,
3441 lpmlcA->pamxctrl[i].szName,
3442 sizeof(lpmlcA->pamxctrl[i].szName), NULL, NULL );
3443 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
3444 * sizeof(mlcW.pamxctrl[i].Bounds) */
3445 memcpy(&lpmlcA->pamxctrl[i].Bounds, &mlcW.pamxctrl[i].Bounds,
3446 sizeof(mlcW.pamxctrl[i].Bounds));
3447 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
3448 * sizeof(mlcW.pamxctrl[i].Metrics) */
3449 memcpy(&lpmlcA->pamxctrl[i].Metrics, &mlcW.pamxctrl[i].Metrics,
3450 sizeof(mlcW.pamxctrl[i].Metrics));
3454 HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
3459 static UINT WINMM_GetVolumeLineControl(WINMM_MMDevice *mmdevice, DWORD line,
3460 MIXERCONTROLW *ctl, DWORD flags)
3462 ctl->dwControlID = (line == 0xFFFF0000) ? 0 : 2;
3463 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
3464 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
3465 ctl->cMultipleItems = 0;
3466 lstrcpyW(ctl->szShortName, volumeW);
3467 lstrcpyW(ctl->szName, volumeW);
3468 ctl->Bounds.s1.dwMinimum = 0;
3469 ctl->Bounds.s1.dwMaximum = 0xFFFF;
3470 ctl->Metrics.cSteps = 192;
3472 return MMSYSERR_NOERROR;
3475 static UINT WINMM_GetMuteLineControl(WINMM_MMDevice *mmdevice, DWORD line,
3476 MIXERCONTROLW *ctl, DWORD flags)
3478 ctl->dwControlID = (line == 0xFFFF0000) ? 1 : 3;
3479 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
3480 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
3481 ctl->cMultipleItems = 0;
3482 lstrcpyW(ctl->szShortName, muteW);
3483 lstrcpyW(ctl->szName, muteW);
3484 ctl->Bounds.s1.dwMinimum = 0;
3485 ctl->Bounds.s1.dwMaximum = 1;
3486 ctl->Metrics.cSteps = 0;
3488 return MMSYSERR_NOERROR;
3491 /**************************************************************************
3492 * mixerGetLineControlsW [WINMM.@]
3494 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
3497 WINMM_MMDevice *mmdevice;
3499 TRACE("(%p, %p, %08x)\n", hmix, lpmlcW, fdwControls);
3501 if(!WINMM_StartDevicesThread())
3502 return MMSYSERR_ERROR;
3504 if(fdwControls & ~(MIXER_GETLINECONTROLSF_ALL |
3505 MIXER_GETLINECONTROLSF_ONEBYID |
3506 MIXER_GETLINECONTROLSF_ONEBYTYPE |
3507 MIXER_OBJECTF_HMIXER |
3508 MIXER_OBJECTF_MIXER)){
3509 WARN("Unknown GetLineControls flag: %x\n", fdwControls);
3510 return MMSYSERR_INVALFLAG;
3513 if(!lpmlcW || lpmlcW->cbStruct < sizeof(*lpmlcW) || !lpmlcW->pamxctrl)
3514 return MMSYSERR_INVALPARAM;
3516 TRACE("dwLineID: %u\n", lpmlcW->dwLineID);
3517 TRACE("dwControl: %x\n", lpmlcW->u.dwControlID);
3518 TRACE("cControls: %u\n", lpmlcW->cControls);
3520 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwControls, NULL);
3522 return MMSYSERR_INVALHANDLE;
3524 switch(fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK){
3525 case MIXER_GETLINECONTROLSF_ALL:
3526 if(lpmlcW->cControls != 2)
3527 return MMSYSERR_INVALPARAM;
3528 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
3529 return MMSYSERR_INVALPARAM;
3530 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
3531 return MIXERR_INVALLINE;
3532 WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
3533 &lpmlcW->pamxctrl[0], fdwControls);
3534 WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
3535 &lpmlcW->pamxctrl[1], fdwControls);
3536 return MMSYSERR_NOERROR;
3537 case MIXER_GETLINECONTROLSF_ONEBYID:
3538 if(lpmlcW->cControls != 1)
3539 return MMSYSERR_INVALPARAM;
3540 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
3541 return MMSYSERR_INVALPARAM;
3542 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
3543 return MIXERR_INVALLINE;
3544 if(lpmlcW->u.dwControlID == 0)
3545 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
3546 lpmlcW->pamxctrl, fdwControls);
3547 if(lpmlcW->u.dwControlID == 1)
3548 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
3549 lpmlcW->pamxctrl, fdwControls);
3550 return MMSYSERR_NOTSUPPORTED;
3551 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
3552 if(lpmlcW->cControls != 1)
3553 return MMSYSERR_INVALPARAM;
3554 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
3555 return MMSYSERR_INVALPARAM;
3556 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
3557 return MIXERR_INVALLINE;
3558 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_VOLUME)
3559 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
3560 lpmlcW->pamxctrl, fdwControls);
3561 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_MUTE)
3562 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
3563 lpmlcW->pamxctrl, fdwControls);
3564 return MMSYSERR_NOTSUPPORTED;
3567 return MMSYSERR_NOTSUPPORTED;
3570 static UINT WINMM_GetSourceLineInfo(WINMM_MMDevice *mmdevice, UINT mmdev_index,
3571 MIXERLINEW *info, DWORD flags)
3574 if(mmdevice->in_caps.szPname[0] != '\0')
3577 if(info->dwSource != 0)
3578 return MIXERR_INVALLINE;
3580 info->dwDestination = 0;
3582 info->fdwLine = MIXERLINE_LINEF_ACTIVE | MIXERLINE_LINEF_SOURCE;
3583 info->cConnections = 0;
3584 info->cControls = 2;
3585 /* volume & mute always affect all channels, so claim 1 channel */
3586 info->cChannels = 1;
3587 info->Target.dwDeviceID = mmdev_index;
3588 info->Target.wMid = ~0;
3589 info->Target.wPid = ~0;
3590 info->Target.vDriverVersion = 0;
3592 lstrcpyW(info->szShortName, volumeW);
3593 lstrcpyW(info->szName, mastervolumeW);
3596 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
3597 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
3598 memcpy(info->Target.szPname, mmdevice->out_caps.szPname,
3599 sizeof(info->Target.szPname));
3601 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_LINE;
3602 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
3603 info->Target.szPname[0] = '\0';
3606 return MMSYSERR_NOERROR;
3609 static UINT WINMM_GetDestinationLineInfo(WINMM_MMDevice *mmdevice,
3610 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
3613 if(mmdevice->in_caps.szPname[0] != '\0')
3616 if(info->dwDestination != 0)
3617 return MIXERR_INVALLINE;
3619 info->dwSource = 0xFFFFFFFF;
3620 info->dwLineID = 0xFFFF0000;
3621 info->fdwLine = MIXERLINE_LINEF_ACTIVE;
3622 info->cConnections = 1;
3623 info->cControls = 2;
3625 lstrcpyW(info->szShortName, volumeW);
3626 lstrcpyW(info->szName, mastervolumeW);
3628 info->Target.dwDeviceID = mmdev_index;
3629 info->Target.wMid = ~0;
3630 info->Target.wPid = ~0;
3631 info->Target.vDriverVersion = 0;
3634 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
3635 info->cChannels = mmdevice->out_caps.wChannels;
3636 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
3637 info->Target.szPname[0] = '\0';
3639 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
3640 info->cChannels = mmdevice->in_caps.wChannels;
3641 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
3642 memcpy(info->Target.szPname, mmdevice->in_caps.szPname,
3643 sizeof(info->Target.szPname));
3646 return MMSYSERR_NOERROR;
3649 static UINT WINMM_GetComponentTypeLineInfo(WINMM_MMDevice *mmdevice,
3650 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
3653 if(mmdevice->in_caps.szPname[0] != '\0')
3656 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_WAVEIN){
3658 return MIXERR_INVALLINE;
3659 info->dwDestination = 0;
3660 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
3663 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_SPEAKERS){
3665 return MIXERR_INVALLINE;
3666 info->dwDestination = 0;
3667 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
3670 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_LINE){
3672 return MIXERR_INVALLINE;
3674 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
3677 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT){
3679 return MIXERR_INVALLINE;
3681 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
3684 TRACE("Returning INVALLINE on this component type: %u\n",
3685 info->dwComponentType);
3687 return MIXERR_INVALLINE;
3690 static UINT WINMM_GetLineIDLineInfo(WINMM_MMDevice *mmdevice,
3691 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
3693 if(info->dwLineID == 0xFFFF0000){
3694 info->dwDestination = 0;
3695 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
3698 if(info->dwLineID == 0){
3700 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
3703 TRACE("Returning INVALLINE on this dwLineID: %u\n", info->dwLineID);
3704 return MIXERR_INVALLINE;
3707 /**************************************************************************
3708 * mixerGetLineInfoW [WINMM.@]
3710 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
3713 WINMM_MMDevice *mmdevice;
3715 TRACE("(%p, %p, %x)\n", hmix, lpmliW, fdwInfo);
3717 if(!WINMM_StartDevicesThread())
3718 return MMSYSERR_ERROR;
3720 if(!lpmliW || lpmliW->cbStruct < sizeof(MIXERLINEW))
3721 return MMSYSERR_INVALPARAM;
3723 TRACE("dwDestination: %u\n", lpmliW->dwDestination);
3724 TRACE("dwSource: %u\n", lpmliW->dwSource);
3725 TRACE("dwLineID: %u\n", lpmliW->dwLineID);
3726 TRACE("fdwLine: 0x%x\n", lpmliW->fdwLine);
3727 TRACE("dwComponentType: 0x%x\n", lpmliW->dwComponentType);
3729 if(fdwInfo & ~(MIXER_GETLINEINFOF_COMPONENTTYPE |
3730 MIXER_GETLINEINFOF_DESTINATION |
3731 MIXER_GETLINEINFOF_LINEID |
3732 MIXER_GETLINEINFOF_SOURCE |
3733 MIXER_GETLINEINFOF_TARGETTYPE |
3734 MIXER_OBJECTF_HMIXER |
3735 MIXER_OBJECTF_MIXER)){
3736 WARN("Unknown GetLineInfo flag: %x\n", fdwInfo);
3737 return MMSYSERR_INVALFLAG;
3740 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwInfo, &mmdev_index);
3742 return MMSYSERR_INVALHANDLE;
3744 switch(fdwInfo & MIXER_GETLINEINFOF_QUERYMASK){
3745 case MIXER_GETLINEINFOF_DESTINATION:
3746 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, lpmliW,
3748 case MIXER_GETLINEINFOF_SOURCE:
3749 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
3750 case MIXER_GETLINEINFOF_COMPONENTTYPE:
3751 return WINMM_GetComponentTypeLineInfo(mmdevice, mmdev_index, lpmliW,
3753 case MIXER_GETLINEINFOF_LINEID:
3754 return WINMM_GetLineIDLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
3755 case MIXER_GETLINEINFOF_TARGETTYPE:
3756 FIXME("TARGETTYPE flag not implemented!\n");
3757 return MIXERR_INVALLINE;
3760 TRACE("Returning INVALFLAG on these flags: %lx\n",
3761 fdwInfo & MIXER_GETLINEINFOF_QUERYMASK);
3762 return MMSYSERR_INVALFLAG;
3765 /**************************************************************************
3766 * mixerGetLineInfoA [WINMM.@]
3768 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
3774 TRACE("(%p, %p, %x)\n", hmix, lpmliA, fdwInfo);
3776 if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
3777 return MMSYSERR_INVALPARAM;
3779 mliW.cbStruct = sizeof(mliW);
3780 switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
3781 case MIXER_GETLINEINFOF_COMPONENTTYPE:
3782 mliW.dwComponentType = lpmliA->dwComponentType;
3784 case MIXER_GETLINEINFOF_DESTINATION:
3785 mliW.dwDestination = lpmliA->dwDestination;
3787 case MIXER_GETLINEINFOF_LINEID:
3788 mliW.dwLineID = lpmliA->dwLineID;
3790 case MIXER_GETLINEINFOF_SOURCE:
3791 mliW.dwDestination = lpmliA->dwDestination;
3792 mliW.dwSource = lpmliA->dwSource;
3794 case MIXER_GETLINEINFOF_TARGETTYPE:
3795 mliW.Target.dwType = lpmliA->Target.dwType;
3796 mliW.Target.wMid = lpmliA->Target.wMid;
3797 mliW.Target.wPid = lpmliA->Target.wPid;
3798 mliW.Target.vDriverVersion = lpmliA->Target.vDriverVersion;
3799 MultiByteToWideChar( CP_ACP, 0, lpmliA->Target.szPname, -1, mliW.Target.szPname, sizeof(mliW.Target.szPname)/sizeof(WCHAR));
3802 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo);
3803 return MMSYSERR_INVALFLAG;
3806 ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
3808 if(ret == MMSYSERR_NOERROR)
3810 lpmliA->dwDestination = mliW.dwDestination;
3811 lpmliA->dwSource = mliW.dwSource;
3812 lpmliA->dwLineID = mliW.dwLineID;
3813 lpmliA->fdwLine = mliW.fdwLine;
3814 lpmliA->dwUser = mliW.dwUser;
3815 lpmliA->dwComponentType = mliW.dwComponentType;
3816 lpmliA->cChannels = mliW.cChannels;
3817 lpmliA->cConnections = mliW.cConnections;
3818 lpmliA->cControls = mliW.cControls;
3819 WideCharToMultiByte( CP_ACP, 0, mliW.szShortName, -1, lpmliA->szShortName,
3820 sizeof(lpmliA->szShortName), NULL, NULL);
3821 WideCharToMultiByte( CP_ACP, 0, mliW.szName, -1, lpmliA->szName,
3822 sizeof(lpmliA->szName), NULL, NULL );
3823 lpmliA->Target.dwType = mliW.Target.dwType;
3824 lpmliA->Target.dwDeviceID = mliW.Target.dwDeviceID;
3825 lpmliA->Target.wMid = mliW.Target.wMid;
3826 lpmliA->Target.wPid = mliW.Target.wPid;
3827 lpmliA->Target.vDriverVersion = mliW.Target.vDriverVersion;
3828 WideCharToMultiByte( CP_ACP, 0, mliW.Target.szPname, -1, lpmliA->Target.szPname,
3829 sizeof(lpmliA->Target.szPname), NULL, NULL );
3834 /**************************************************************************
3835 * mixerSetControlDetails [WINMM.@]
3837 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
3840 WINMM_ControlDetails details;
3842 TRACE("(%p, %p, %x)\n", hmix, lpmcd, fdwDetails);
3844 if(!WINMM_StartDevicesThread())
3845 return MMSYSERR_ERROR;
3847 if((fdwDetails & MIXER_SETCONTROLDETAILSF_QUERYMASK) ==
3848 MIXER_SETCONTROLDETAILSF_CUSTOM)
3849 return MMSYSERR_NOTSUPPORTED;
3852 return MMSYSERR_INVALPARAM;
3854 TRACE("dwControlID: %u\n", lpmcd->dwControlID);
3856 details.hmix = hmix;
3857 details.details = lpmcd;
3858 details.flags = fdwDetails;
3860 return SendMessageW(g_devices_hwnd, MXDM_SETCONTROLDETAILS,
3861 (DWORD_PTR)&details, 0);
3864 /**************************************************************************
3865 * mixerMessage [WINMM.@]
3867 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3869 TRACE("(%p, %d, %lx, %lx)\n", hmix, uMsg, dwParam1, dwParam2);
3871 return MMSYSERR_NOTSUPPORTED;