dsound: Remove hardware acceleration support.
[wine] / dlls / winmm / waveform.c
1 /*
2  * Copyright 1993      Martin Ayotte
3  *           1998-2002 Eric Pouech
4  *           2011 Andrew Eikum for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
24
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #define COBJMACROS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "mmsystem.h"
32 #include "mmreg.h"
33 #include "msacm.h"
34 #include "winuser.h"
35 #include "winnls.h"
36 #include "winternl.h"
37
38 #include "winemm.h"
39
40 #include "ole2.h"
41 #include "initguid.h"
42 #include "devpkey.h"
43 #include "mmdeviceapi.h"
44 #include "audioclient.h"
45 #include "audiopolicy.h"
46
47 #include "wine/debug.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
50
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',
54     'u','m','e',0};
55 static const WCHAR muteW[] = {'M','u','t','e',0};
56
57 /* HWAVE (and HMIXER) format:
58  *
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
65  *
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.
69  */
70
71 #define MAX_DEVICES 256
72
73 typedef struct _WINMM_CBInfo {
74     DWORD_PTR callback;
75     DWORD_PTR user;
76     DWORD flags;
77     HWAVE hwave;
78 } WINMM_CBInfo;
79
80 struct _WINMM_MMDevice;
81 typedef struct _WINMM_MMDevice WINMM_MMDevice;
82
83 typedef struct _WINMM_Device {
84     WINMM_CBInfo cb_info;
85
86     HWAVE handle;
87
88     BOOL open;
89
90     IMMDevice *device;
91     IAudioClient *client;
92     IAudioRenderClient *render;
93     IAudioCaptureClient *capture;
94     IAudioClock *clock;
95     IAudioStreamVolume *volume;
96
97     HACMSTREAM acm_handle;
98     ACMSTREAMHEADER acm_hdr;
99     UINT32 acm_offs;
100
101     WAVEHDR *first, *last, *playing, *loop_start;
102
103     BOOL stopped;
104     DWORD loop_counter;
105     UINT32 bytes_per_frame, samples_per_sec, ofs_bytes, played_frames;
106
107     /* stored in frames of sample rate, *not* AC::GetFrequency */
108     UINT64 last_clock_pos;
109
110     HANDLE event;
111     CRITICAL_SECTION lock;
112
113     WINMM_MMDevice *parent;
114 } WINMM_Device;
115
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*/
119     WCHAR *dev_id;
120
121     ISimpleAudioVolume *volume;
122
123     GUID session;
124
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 */
127     UINT mixer_count;
128
129     CRITICAL_SECTION lock;
130
131     WINMM_Device *devices[MAX_DEVICES];
132 };
133
134 static WINMM_MMDevice *g_out_mmdevices;
135 static UINT g_outmmdevices_count;
136
137 static WINMM_MMDevice *g_in_mmdevices;
138 static UINT g_inmmdevices_count;
139
140 static IMMDeviceEnumerator *g_devenum;
141
142 static CRITICAL_SECTION g_devthread_lock;
143 static HANDLE g_devices_thread;
144 static HWND g_devices_hwnd;
145
146 static UINT g_devhandle_count;
147 static HANDLE *g_device_handles;
148 static WINMM_Device **g_handle_devices;
149
150 typedef struct _WINMM_OpenInfo {
151     HWAVE handle;
152     UINT req_device;
153     WAVEFORMATEX *format;
154     DWORD_PTR callback;
155     DWORD_PTR cb_user;
156     DWORD flags;
157 } WINMM_OpenInfo;
158
159 typedef struct _WINMM_ControlDetails {
160     HMIXEROBJ hmix;
161     MIXERCONTROLDETAILS *details;
162     DWORD flags;
163 } WINMM_ControlDetails;
164
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);
169
170 BOOL WINMM_InitWaveform(void)
171 {
172     InitializeCriticalSection(&g_devthread_lock);
173     return TRUE;
174 }
175
176 static inline HWAVE WINMM_MakeHWAVE(UINT mmdevice, BOOL is_out, UINT device)
177 {
178     return ULongToHandle((1 << 15) | ((!!is_out) << 14) |
179             (mmdevice << 8) | device);
180 }
181
182 static inline void WINMM_DecomposeHWAVE(HWAVE hwave, UINT *mmdevice_index,
183         BOOL *is_out, UINT *device_index, UINT *junk)
184 {
185     ULONG32 l = HandleToULong(hwave);
186     *device_index = l & 0xFF;
187     *mmdevice_index = (l >> 8) & 0x3F;
188     *is_out = (l >> 14) & 0x1;
189     *junk = l >> 15;
190 }
191
192 static void WINMM_InitDevice(WINMM_Device *device,
193         WINMM_MMDevice *parent, HWAVE hwave)
194 {
195     InitializeCriticalSection(&device->lock);
196     device->handle = hwave;
197     device->parent = parent;
198 }
199
200 /* finds the first unused Device, marks it as "open", and returns
201  * a pointer to the device
202  *
203  * IMPORTANT: it is the caller's responsibility to release the device's lock
204  * on success
205  */
206 static WINMM_Device *WINMM_FindUnusedDevice(BOOL is_out, UINT mmdevice_index)
207 {
208     WINMM_MMDevice *mmdevice;
209     UINT i;
210
211     if(is_out)
212         mmdevice = &g_out_mmdevices[mmdevice_index];
213     else
214         mmdevice = &g_in_mmdevices[mmdevice_index];
215
216     EnterCriticalSection(&mmdevice->lock);
217     for(i = 0; i < MAX_DEVICES; ++i){
218         WINMM_Device *device = mmdevice->devices[i];
219
220         if(!device){
221             device = mmdevice->devices[i] = HeapAlloc(GetProcessHeap(),
222                     HEAP_ZERO_MEMORY, sizeof(WINMM_Device));
223             if(!device){
224                 LeaveCriticalSection(&mmdevice->lock);
225                 return NULL;
226             }
227
228             WINMM_InitDevice(device, mmdevice,
229                     WINMM_MakeHWAVE(mmdevice_index, is_out, i));
230             EnterCriticalSection(&device->lock);
231         }else
232             EnterCriticalSection(&device->lock);
233
234         if(!device->open){
235             LeaveCriticalSection(&mmdevice->lock);
236             device->open = TRUE;
237             TRACE("Found free device: mmdevice: %u, device id: %u\n",
238                     mmdevice_index, i);
239             return device;
240         }
241
242         LeaveCriticalSection(&device->lock);
243     }
244
245     LeaveCriticalSection(&mmdevice->lock);
246
247     TRACE("All devices in use: mmdevice: %u\n", mmdevice_index);
248
249     return NULL;
250 }
251
252 static inline BOOL WINMM_ValidateAndLock(WINMM_Device *device)
253 {
254     if(!device)
255         return FALSE;
256
257     EnterCriticalSection(&device->lock);
258
259     if(!device->open){
260         LeaveCriticalSection(&device->lock);
261         return FALSE;
262     }
263
264     return TRUE;
265 }
266
267 static WINMM_Device *WINMM_GetDeviceFromHWAVE(HWAVE hwave)
268 {
269     WINMM_MMDevice *mmdevice;
270     WINMM_Device *device;
271     UINT mmdevice_index, device_index, junk;
272     BOOL is_out;
273
274     WINMM_DecomposeHWAVE(hwave, &mmdevice_index, &is_out, &device_index, &junk);
275
276     if(junk != 0x1)
277         return NULL;
278
279     if(mmdevice_index >= (is_out ? g_outmmdevices_count : g_inmmdevices_count))
280         return NULL;
281
282     if(is_out)
283         mmdevice = &g_out_mmdevices[mmdevice_index];
284     else
285         mmdevice = &g_in_mmdevices[mmdevice_index];
286
287     EnterCriticalSection(&mmdevice->lock);
288
289     device = mmdevice->devices[device_index];
290
291     LeaveCriticalSection(&mmdevice->lock);
292
293     return device;
294 }
295
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,
299         DWORD_PTR param2)
300 {
301     DriverCallback(info->callback, info->flags, (HDRVR)info->hwave,
302         msg, info->user, param1, param2);
303 }
304
305 static HRESULT WINMM_GetFriendlyName(IMMDevice *device, WCHAR *out,
306         UINT outlen)
307 {
308     IPropertyStore *ps;
309     PROPVARIANT var;
310     HRESULT hr;
311
312     hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
313     if(FAILED(hr))
314         return hr;
315
316     PropVariantInit(&var);
317
318     hr = IPropertyStore_GetValue(ps,
319             (PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &var);
320     if(FAILED(hr)){
321         IPropertyStore_Release(ps);
322         return hr;
323     }
324
325     lstrcpynW(out, var.u.pwszVal, outlen);
326
327     PropVariantClear(&var);
328
329     IPropertyStore_Release(ps);
330
331     return S_OK;
332 }
333
334 static HRESULT WINMM_TestFormat(IAudioClient *client, DWORD rate, DWORD depth,
335         WORD channels)
336 {
337     WAVEFORMATEX fmt, *junk;
338     HRESULT hr;
339
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;
346     fmt.cbSize = 0;
347
348     hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED,
349             &fmt, &junk);
350     if(SUCCEEDED(hr))
351         CoTaskMemFree(junk);
352
353     return hr;
354 }
355
356 static struct _TestFormat {
357     DWORD flag;
358     DWORD rate;
359     DWORD depth;
360     WORD channels;
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 },
382     {0}
383 };
384
385 static DWORD WINMM_GetSupportedFormats(IMMDevice *device)
386 {
387     DWORD flags = 0;
388     HRESULT hr;
389     struct _TestFormat *fmt;
390     IAudioClient *client;
391
392     hr = IMMDevice_Activate(device, &IID_IAudioClient,
393             CLSCTX_INPROC_SERVER, NULL, (void**)&client);
394     if(FAILED(hr))
395         return 0;
396
397     for(fmt = formats_to_test; fmt->flag; ++fmt){
398         hr = WINMM_TestFormat(client, fmt->rate, fmt->depth, fmt->channels);
399         if(hr == S_OK)
400             flags |= fmt->flag;
401     }
402
403     IAudioClient_Release(client);
404
405     return flags;
406 }
407
408 static HRESULT WINMM_InitMMDevice(EDataFlow flow, IMMDevice *device,
409         WINMM_MMDevice *dev, UINT index)
410 {
411     HRESULT hr;
412
413     if(flow == eRender){
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';
423
424         hr = WINMM_GetFriendlyName(device, dev->out_caps.szPname,
425                 sizeof(dev->out_caps.szPname) /
426                 sizeof(*dev->out_caps.szPname));
427         if(FAILED(hr))
428             return hr;
429     }else{
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';
437
438         hr = WINMM_GetFriendlyName(device, dev->in_caps.szPname,
439                 sizeof(dev->in_caps.szPname) /
440                 sizeof(*dev->in_caps.szPname));
441         if(FAILED(hr))
442             return hr;
443     }
444
445     hr = IMMDevice_GetId(device, &dev->dev_id);
446     if(FAILED(hr))
447         return hr;
448
449     CoCreateGuid(&dev->session);
450
451     InitializeCriticalSection(&dev->lock);
452
453     return S_OK;
454 }
455
456 static HRESULT WINMM_EnumDevices(WINMM_MMDevice **devices, UINT *devcount,
457         EDataFlow flow)
458 {
459     IMMDeviceCollection *devcoll;
460     HRESULT hr;
461
462     hr = IMMDeviceEnumerator_EnumAudioEndpoints(g_devenum, flow,
463             DEVICE_STATE_ACTIVE, &devcoll);
464     if(FAILED(hr))
465         return hr;
466
467     hr = IMMDeviceCollection_GetCount(devcoll, devcount);
468     if(FAILED(hr)){
469         IMMDeviceCollection_Release(devcoll);
470         return hr;
471     }
472
473     if(*devcount > 0){
474         UINT n, count;
475         IMMDevice *def_dev = NULL;
476
477         *devices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
478                 sizeof(WINMM_MMDevice) * (*devcount));
479         if(!*devices){
480             IMMDeviceCollection_Release(devcoll);
481             return E_OUTOFMEMORY;
482         }
483
484         count = 0;
485
486         /* make sure that device 0 is the default device */
487         hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(g_devenum,
488                 flow, eConsole, &def_dev);
489         if(SUCCEEDED(hr)){
490             WINMM_InitMMDevice(flow, def_dev, &(*devices)[0], 0);
491             count = 1;
492         }
493
494         for(n = 0; n < *devcount; ++n){
495             IMMDevice *device;
496
497             hr = IMMDeviceCollection_Item(devcoll, n, &device);
498             if(SUCCEEDED(hr)){
499                 if(device != def_dev){
500                     WINMM_InitMMDevice(flow, device, &(*devices)[count], count);
501                     ++count;
502                 }
503
504                 IMMDevice_Release(device);
505             }
506         }
507
508         if(def_dev)
509             IMMDevice_Release(def_dev);
510
511         *devcount = count;
512     }
513
514     IMMDeviceCollection_Release(devcoll);
515
516     return S_OK;
517 }
518
519 static HRESULT WINMM_InitMMDevices(void)
520 {
521     HRESULT hr;
522
523     hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
524             CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&g_devenum);
525     if(FAILED(hr))
526         return hr;
527
528     hr = WINMM_EnumDevices(&g_out_mmdevices, &g_outmmdevices_count, eRender);
529     if(FAILED(hr)){
530         g_outmmdevices_count = 0;
531         g_inmmdevices_count = 0;
532         return hr;
533     }
534
535     hr = WINMM_EnumDevices(&g_in_mmdevices, &g_inmmdevices_count, eCapture);
536     if(FAILED(hr)){
537         g_inmmdevices_count = 0;
538         return hr;
539     }
540
541     return S_OK;
542 }
543
544 static inline BOOL WINMM_IsMapper(UINT device)
545 {
546     return (device == WAVE_MAPPER || device == (UINT16)WAVE_MAPPER);
547 }
548
549 static MMRESULT WINMM_TryDeviceMapping(WINMM_OpenInfo *info, WORD channels,
550         DWORD freq, DWORD bits_per_samp, BOOL is_out)
551 {
552     WINMM_Device *device;
553     WAVEFORMATEX target;
554     MMRESULT mr;
555     UINT i;
556
557     TRACE("format: %u, channels: %u, sample rate: %u, bit depth: %u\n",
558             WAVE_FORMAT_PCM, channels, freq, bits_per_samp);
559
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;
566     target.cbSize = 0;
567
568     if(is_out)
569         mr = acmStreamOpen(NULL, NULL, info->format, &target, NULL, 0,
570                 0, ACM_STREAMOPENF_QUERY);
571     else
572         mr = acmStreamOpen(NULL, NULL, &target, info->format, NULL, 0,
573                 0, ACM_STREAMOPENF_QUERY);
574     if(mr != MMSYSERR_NOERROR)
575         return mr;
576
577     /* ACM can convert from src->dst, so try to find a device
578      * that supports dst */
579     if(is_out){
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 = &target;
585                 mr = WOD_Open(&l_info);
586                 if(mr == MMSYSERR_NOERROR){
587                     info->handle = l_info.handle;
588                     break;
589                 }
590             }
591         }else{
592             WINMM_OpenInfo l_info = *info;
593             l_info.flags &= ~WAVE_MAPPED;
594             l_info.format = &target;
595             mr = WOD_Open(&l_info);
596             if(mr == MMSYSERR_NOERROR)
597                 info->handle = l_info.handle;
598         }
599     }else{
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 = &target;
605                 mr = WID_Open(&l_info);
606                 if(mr == MMSYSERR_NOERROR){
607                     info->handle = l_info.handle;
608                     break;
609                 }
610             }
611         }else{
612             WINMM_OpenInfo l_info = *info;
613             l_info.flags &= ~WAVE_MAPPED;
614             l_info.format = &target;
615             mr = WID_Open(&l_info);
616             if(mr == MMSYSERR_NOERROR)
617                 info->handle = l_info.handle;
618         }
619     }
620     if(mr != MMSYSERR_NOERROR)
621         return WAVERR_BADFORMAT;
622
623     device = WINMM_GetDeviceFromHWAVE(info->handle);
624     if(!device)
625         return MMSYSERR_INVALHANDLE;
626
627     /* set up the ACM stream */
628     if(is_out)
629         mr = acmStreamOpen(&device->acm_handle, NULL, info->format, &target,
630                 NULL, 0, 0, 0);
631     else
632         mr = acmStreamOpen(&device->acm_handle, NULL, &target, info->format,
633                 NULL, 0, 0, 0);
634     if(mr != MMSYSERR_NOERROR){
635         if(is_out)
636             WOD_Close((HWAVEOUT)info->handle);
637         else
638             WID_Close((HWAVEIN)info->handle);
639         return mr;
640     }
641
642     TRACE("Success\n");
643     return MMSYSERR_NOERROR;
644 }
645
646 static MMRESULT WINMM_MapDevice(WINMM_OpenInfo *info, BOOL is_out)
647 {
648     UINT i;
649     MMRESULT mr;
650     WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)info->format;
651
652     TRACE("(%p, %d)\n", info, is_out);
653
654     /* try to find a direct match */
655     if(is_out){
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;
663                     return mr;
664                 }
665             }
666         }else{
667             l_info.flags &= ~WAVE_MAPPED;
668             mr = WOD_Open(&l_info);
669             if(mr == MMSYSERR_NOERROR){
670                 info->handle = l_info.handle;
671                 return mr;
672             }
673         }
674     }else{
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;
682                     return mr;
683                 }
684             }
685         }else{
686             l_info.flags &= ~WAVE_MAPPED;
687             mr = WID_Open(&l_info);
688             if(mr == MMSYSERR_NOERROR){
689                 info->handle = l_info.handle;
690                 return mr;
691             }
692         }
693     }
694
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)
703             return mr;
704
705         mr = WINMM_TryDeviceMapping(info, info->format->nChannels,
706                 info->format->nSamplesPerSec, 8, is_out);
707         if(mr == MMSYSERR_NOERROR)
708             return mr;
709     }else{
710         WORD channels;
711
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)
717             return mr;
718         mr = WINMM_TryDeviceMapping(info, channels,
719                 info->format->nSamplesPerSec, 8, is_out);
720         if(mr == MMSYSERR_NOERROR)
721             return mr;
722
723         channels = (channels == 2) ? 1 : 2;
724         mr = WINMM_TryDeviceMapping(info, channels,
725                 info->format->nSamplesPerSec, 16, is_out);
726         if(mr == MMSYSERR_NOERROR)
727             return mr;
728         mr = WINMM_TryDeviceMapping(info, channels,
729                 info->format->nSamplesPerSec, 8, is_out);
730         if(mr == MMSYSERR_NOERROR)
731             return mr;
732
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)
737             return mr;
738         mr = WINMM_TryDeviceMapping(info, channels, 48000, 16, is_out);
739         if(mr == MMSYSERR_NOERROR)
740             return mr;
741         mr = WINMM_TryDeviceMapping(info, channels, 44100, 16, is_out);
742         if(mr == MMSYSERR_NOERROR)
743             return mr;
744         mr = WINMM_TryDeviceMapping(info, channels, 22050, 16, is_out);
745         if(mr == MMSYSERR_NOERROR)
746             return mr;
747         mr = WINMM_TryDeviceMapping(info, channels, 11025, 16, is_out);
748         if(mr == MMSYSERR_NOERROR)
749             return mr;
750
751         channels = (channels == 2) ? 1 : 2;
752         mr = WINMM_TryDeviceMapping(info, channels, 96000, 16, is_out);
753         if(mr == MMSYSERR_NOERROR)
754             return mr;
755         mr = WINMM_TryDeviceMapping(info, channels, 48000, 16, is_out);
756         if(mr == MMSYSERR_NOERROR)
757             return mr;
758         mr = WINMM_TryDeviceMapping(info, channels, 44100, 16, is_out);
759         if(mr == MMSYSERR_NOERROR)
760             return mr;
761         mr = WINMM_TryDeviceMapping(info, channels, 22050, 16, is_out);
762         if(mr == MMSYSERR_NOERROR)
763             return mr;
764         mr = WINMM_TryDeviceMapping(info, channels, 11025, 16, is_out);
765         if(mr == MMSYSERR_NOERROR)
766             return mr;
767
768         channels = info->format->nChannels;
769         mr = WINMM_TryDeviceMapping(info, channels, 96000, 8, is_out);
770         if(mr == MMSYSERR_NOERROR)
771             return mr;
772         mr = WINMM_TryDeviceMapping(info, channels, 48000, 8, is_out);
773         if(mr == MMSYSERR_NOERROR)
774             return mr;
775         mr = WINMM_TryDeviceMapping(info, channels, 44100, 8, is_out);
776         if(mr == MMSYSERR_NOERROR)
777             return mr;
778         mr = WINMM_TryDeviceMapping(info, channels, 22050, 8, is_out);
779         if(mr == MMSYSERR_NOERROR)
780             return mr;
781         mr = WINMM_TryDeviceMapping(info, channels, 11025, 8, is_out);
782         if(mr == MMSYSERR_NOERROR)
783             return mr;
784
785         channels = (channels == 2) ? 1 : 2;
786         mr = WINMM_TryDeviceMapping(info, channels, 96000, 8, is_out);
787         if(mr == MMSYSERR_NOERROR)
788             return mr;
789         mr = WINMM_TryDeviceMapping(info, channels, 48000, 8, is_out);
790         if(mr == MMSYSERR_NOERROR)
791             return mr;
792         mr = WINMM_TryDeviceMapping(info, channels, 44100, 8, is_out);
793         if(mr == MMSYSERR_NOERROR)
794             return mr;
795         mr = WINMM_TryDeviceMapping(info, channels, 22050, 8, is_out);
796         if(mr == MMSYSERR_NOERROR)
797             return mr;
798         mr = WINMM_TryDeviceMapping(info, channels, 11025, 8, is_out);
799         if(mr == MMSYSERR_NOERROR)
800             return mr;
801     }
802
803     WARN("Unable to find compatible device!\n");
804     return WAVERR_BADFORMAT;
805 }
806
807 static LRESULT WINMM_OpenDevice(WINMM_Device *device, WINMM_MMDevice *mmdevice,
808         WINMM_OpenInfo *info)
809 {
810     WAVEFORMATEX *closer_fmt = NULL, fmt, *passed_fmt;
811     LRESULT ret = MMSYSERR_ERROR;
812     HRESULT hr;
813
814     hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id,
815             &device->device);
816     if(FAILED(hr)){
817         ERR("Device %s (%s) unavailable: %08x\n",
818                 wine_dbgstr_w(mmdevice->dev_id),
819                 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
820         goto error;
821     }
822
823     hr = IMMDevice_Activate(device->device, &IID_IAudioClient,
824             CLSCTX_INPROC_SERVER, NULL, (void**)&device->client);
825     if(FAILED(hr)){
826         ERR("Activate failed: %08x\n", hr);
827         goto error;
828     }
829
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 */
833         passed_fmt = &fmt;
834         memcpy(passed_fmt, info->format, sizeof(PCMWAVEFORMAT));
835         passed_fmt->cbSize = 0;
836     }else
837         passed_fmt = info->format;
838
839     hr = IAudioClient_IsFormatSupported(device->client,
840             AUDCLNT_SHAREMODE_SHARED, passed_fmt, &closer_fmt);
841     if(closer_fmt)
842         CoTaskMemFree(closer_fmt);
843     if(FAILED(hr) && hr != AUDCLNT_E_UNSUPPORTED_FORMAT){
844         ERR("IsFormatSupported failed: %08x\n", hr);
845         goto error;
846     }
847     if(hr == S_FALSE || hr == AUDCLNT_E_UNSUPPORTED_FORMAT){
848         ret = WAVERR_BADFORMAT;
849         goto error;
850     }
851     if(info->flags & WAVE_FORMAT_QUERY){
852         ret = MMSYSERR_NOERROR;
853         goto error;
854     }
855
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);
860     if(FAILED(hr)){
861         ERR("Initialize failed: %08x\n", hr);
862         goto error;
863     }
864
865     hr = IAudioClient_GetService(device->client, &IID_IAudioClock,
866             (void**)&device->clock);
867     if(FAILED(hr)){
868         ERR("GetService failed: %08x\n", hr);
869         goto error;
870     }
871
872     if(!device->event){
873         device->event = CreateEventW(NULL, FALSE, FALSE, NULL);
874         if(!device->event){
875             ERR("CreateEvent failed: %08x\n", hr);
876             goto error;
877         }
878
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));
886         }else{
887             g_device_handles = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE));
888             g_handle_devices = HeapAlloc(GetProcessHeap(), 0,
889                     sizeof(WINMM_Device *));
890         }
891         g_device_handles[g_devhandle_count] = device->event;
892         g_handle_devices[g_devhandle_count] = device;
893         ++g_devhandle_count;
894     }
895
896     hr = IAudioClient_SetEventHandle(device->client, device->event);
897     if(FAILED(hr)){
898         ERR("SetEventHandle failed: %08x\n", hr);
899         goto error;
900     }
901
902     device->bytes_per_frame = info->format->nBlockAlign;
903     device->samples_per_sec = info->format->nSamplesPerSec;
904
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;
911
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;
916
917     info->handle = device->handle;
918
919     return MMSYSERR_NOERROR;
920
921 error:
922     if(device->client){
923         IAudioClient_Release(device->client);
924         device->client = NULL;
925     }
926     if(device->device){
927         IMMDevice_Release(device->device);
928         device->device = NULL;
929     }
930
931     return ret;
932 }
933
934 static LRESULT WOD_Open(WINMM_OpenInfo *info)
935 {
936     WINMM_MMDevice *mmdevice;
937     WINMM_Device *device = NULL;
938     LRESULT ret = MMSYSERR_ERROR;
939     HRESULT hr;
940
941     TRACE("(%u, %p, %08x)\n", info->req_device, info, info->flags);
942
943     if(WINMM_IsMapper(info->req_device) || (info->flags & WAVE_MAPPED))
944         return WINMM_MapDevice(info, TRUE);
945
946     if(info->req_device >= g_outmmdevices_count)
947         return MMSYSERR_BADDEVICEID;
948
949     mmdevice = &g_out_mmdevices[info->req_device];
950
951     if(!mmdevice->out_caps.szPname[0])
952         return MMSYSERR_NOTENABLED;
953
954     device = WINMM_FindUnusedDevice(TRUE, info->req_device);
955     if(!device)
956         return MMSYSERR_ALLOCATED;
957
958     ret = WINMM_OpenDevice(device, mmdevice, info);
959     if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
960         goto error;
961     ret = MMSYSERR_ERROR;
962
963     hr = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
964             (void**)&device->render);
965     if(FAILED(hr)){
966         ERR("GetService failed: %08x\n", hr);
967         goto error;
968     }
969
970     hr = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
971             (void**)&device->volume);
972     if(FAILED(hr)){
973         ERR("GetService failed: %08x\n", hr);
974         goto error;
975     }
976
977     LeaveCriticalSection(&device->lock);
978
979     return MMSYSERR_NOERROR;
980
981 error:
982     if(device->device){
983         IMMDevice_Release(device->device);
984         device->device = NULL;
985     }
986     if(device->client){
987         IAudioClient_Release(device->client);
988         device->client = NULL;
989     }
990     if(device->render){
991         IAudioRenderClient_Release(device->render);
992         device->render = NULL;
993     }
994     if(device->volume){
995         IAudioStreamVolume_Release(device->volume);
996         device->volume = NULL;
997     }
998     if(device->clock){
999         IAudioClock_Release(device->clock);
1000         device->clock = NULL;
1001     }
1002     device->open = FALSE;
1003     LeaveCriticalSection(&device->lock);
1004     return ret;
1005 }
1006
1007 static LRESULT WID_Open(WINMM_OpenInfo *info)
1008 {
1009     WINMM_MMDevice *mmdevice;
1010     WINMM_Device *device = NULL;
1011     LRESULT ret = MMSYSERR_ERROR;
1012     HRESULT hr;
1013
1014     TRACE("(%u, %p, %08x)\n", info->req_device, info, info->flags);
1015
1016     if(WINMM_IsMapper(info->req_device) || info->flags & WAVE_MAPPED)
1017         return WINMM_MapDevice(info, FALSE);
1018
1019     if(info->req_device >= g_inmmdevices_count)
1020         return MMSYSERR_BADDEVICEID;
1021
1022     mmdevice = &g_in_mmdevices[info->req_device];
1023
1024     if(!mmdevice->in_caps.szPname[0])
1025         return MMSYSERR_NOTENABLED;
1026
1027     device = WINMM_FindUnusedDevice(FALSE, info->req_device);
1028     if(!device)
1029         return MMSYSERR_ALLOCATED;
1030
1031     ret = WINMM_OpenDevice(device, mmdevice, info);
1032     if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1033         goto error;
1034     ret = MMSYSERR_ERROR;
1035
1036     hr = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
1037             (void**)&device->capture);
1038     if(FAILED(hr)){
1039         ERR("GetService failed: %08x\n", hr);
1040         goto error;
1041     }
1042
1043     LeaveCriticalSection(&device->lock);
1044
1045     return MMSYSERR_NOERROR;
1046
1047 error:
1048     if(device->device){
1049         IMMDevice_Release(device->device);
1050         device->device = NULL;
1051     }
1052     if(device->client){
1053         IAudioClient_Release(device->client);
1054         device->client = NULL;
1055     }
1056     if(device->capture){
1057         IAudioCaptureClient_Release(device->capture);
1058         device->capture = NULL;
1059     }
1060     if(device->clock){
1061         IAudioClock_Release(device->clock);
1062         device->clock = NULL;
1063     }
1064     device->open = FALSE;
1065     LeaveCriticalSection(&device->lock);
1066     return ret;
1067 }
1068
1069 static HRESULT WINMM_CloseDevice(WINMM_Device *device)
1070 {
1071     device->open = FALSE;
1072
1073     if(!device->stopped){
1074         IAudioClient_Stop(device->client);
1075         device->stopped = TRUE;
1076     }
1077
1078     if(device->acm_handle){
1079         acmStreamClose(device->acm_handle, 0);
1080         device->acm_handle = NULL;
1081     }
1082
1083     IMMDevice_Release(device->device);
1084     device->device = NULL;
1085
1086     IAudioClient_Release(device->client);
1087     device->client = NULL;
1088
1089     IAudioClock_Release(device->clock);
1090     device->clock = NULL;
1091
1092     return S_OK;
1093 }
1094
1095 static LRESULT WOD_Close(HWAVEOUT hwave)
1096 {
1097     WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1098
1099     TRACE("(%p)\n", hwave);
1100
1101     if(!WINMM_ValidateAndLock(device))
1102         return MMSYSERR_INVALHANDLE;
1103
1104     WINMM_CloseDevice(device);
1105
1106     IAudioRenderClient_Release(device->render);
1107     device->render = NULL;
1108
1109     IAudioStreamVolume_Release(device->volume);
1110     device->volume = NULL;
1111
1112     LeaveCriticalSection(&device->lock);
1113
1114     return MMSYSERR_NOERROR;
1115 }
1116
1117 static LRESULT WID_Close(HWAVEIN hwave)
1118 {
1119     WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1120
1121     TRACE("(%p)\n", hwave);
1122
1123     if(!WINMM_ValidateAndLock(device))
1124         return MMSYSERR_INVALHANDLE;
1125
1126     WINMM_CloseDevice(device);
1127
1128     IAudioCaptureClient_Release(device->capture);
1129     device->capture = NULL;
1130
1131     LeaveCriticalSection(&device->lock);
1132
1133     return MMSYSERR_NOERROR;
1134 }
1135
1136 static LRESULT WINMM_PrepareHeader(HWAVE hwave, WAVEHDR *header)
1137 {
1138     WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1139
1140     TRACE("(%p, %p)\n", hwave, header);
1141
1142     if(!WINMM_ValidateAndLock(device))
1143         return MMSYSERR_INVALHANDLE;
1144
1145     if(device->render && device->acm_handle){
1146         ACMSTREAMHEADER *ash;
1147         DWORD size;
1148         MMRESULT mr;
1149
1150         mr = acmStreamSize(device->acm_handle, header->dwBufferLength, &size,
1151                 ACM_STREAMSIZEF_SOURCE);
1152         if(mr != MMSYSERR_NOERROR){
1153             LeaveCriticalSection(&device->lock);
1154             return mr;
1155         }
1156
1157         ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + size);
1158         if(!ash){
1159             LeaveCriticalSection(&device->lock);
1160             return MMSYSERR_NOMEM;
1161         }
1162
1163         ash->cbStruct = sizeof(*ash);
1164         ash->fdwStatus = 0;
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;
1171         ash->dwDstUser = 0;
1172
1173         mr = acmStreamPrepareHeader(device->acm_handle, ash, 0);
1174         if(mr != MMSYSERR_NOERROR){
1175             HeapFree(GetProcessHeap(), 0, ash);
1176             LeaveCriticalSection(&device->lock);
1177             return mr;
1178         }
1179
1180         header->reserved = (DWORD_PTR)ash;
1181     }
1182
1183     LeaveCriticalSection(&device->lock);
1184
1185     header->dwFlags |= WHDR_PREPARED;
1186     header->dwFlags &= ~WHDR_DONE;
1187
1188     return MMSYSERR_NOERROR;
1189 }
1190
1191 static LRESULT WINMM_UnprepareHeader(HWAVE hwave, WAVEHDR *header)
1192 {
1193     WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1194
1195     TRACE("(%p, %p)\n", hwave, header);
1196
1197     if(!WINMM_ValidateAndLock(device))
1198         return MMSYSERR_INVALHANDLE;
1199
1200     if(device->render && device->acm_handle){
1201         ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1202
1203         acmStreamUnprepareHeader(device->acm_handle, ash, 0);
1204
1205         HeapFree(GetProcessHeap(), 0, ash);
1206     }
1207
1208     LeaveCriticalSection(&device->lock);
1209
1210     header->dwFlags &= ~WHDR_PREPARED;
1211     header->dwFlags |= WHDR_DONE;
1212
1213     return MMSYSERR_NOERROR;
1214 }
1215
1216 static UINT32 WINMM_HeaderLenBytes(WINMM_Device *device, WAVEHDR *header)
1217 {
1218     if(device->acm_handle){
1219         ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1220         return ash->cbDstLengthUsed;
1221     }
1222
1223     return header->dwBufferLength;
1224 }
1225
1226 static UINT32 WINMM_HeaderLenFrames(WINMM_Device *device, WAVEHDR *header)
1227 {
1228     return WINMM_HeaderLenBytes(device, header) / device->bytes_per_frame;
1229 }
1230
1231 static WAVEHDR *WOD_MarkDoneHeaders(WINMM_Device *device)
1232 {
1233     HRESULT hr;
1234     WAVEHDR *queue, *first = device->first;
1235     UINT64 clock_freq, clock_pos, clock_frames;
1236     UINT32 nloops, queue_frames = 0;
1237
1238     hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
1239     if(FAILED(hr)){
1240         ERR("GetFrequency failed: %08x\n", hr);
1241         return first;
1242     }
1243
1244     hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
1245     if(FAILED(hr)){
1246         ERR("GetPosition failed: %08x\n", hr);
1247         return first;
1248     }
1249
1250     clock_frames = (clock_pos / (double)clock_freq) * device->samples_per_sec;
1251
1252     first = queue = device->first;
1253     nloops = device->loop_counter;
1254     while(queue &&
1255             (queue_frames += WINMM_HeaderLenFrames(device, queue)) <=
1256                 clock_frames - device->last_clock_pos){
1257         if(!nloops){
1258             device->first->dwFlags &= ~WHDR_INQUEUE;
1259             device->first->dwFlags |= WHDR_DONE;
1260             device->last_clock_pos += queue_frames;
1261             queue_frames = 0;
1262             queue = device->first = queue->lpNext;
1263         }else{
1264             if(queue->dwFlags & WHDR_BEGINLOOP){
1265                 if(queue->dwFlags & WHDR_ENDLOOP)
1266                     --nloops;
1267                 else
1268                     queue = queue->lpNext;
1269             }else if(queue->dwFlags & WHDR_ENDLOOP){
1270                 queue = device->loop_start;
1271                 --nloops;
1272             }
1273         }
1274     }
1275
1276     return first;
1277 }
1278
1279 static void WOD_PushData(WINMM_Device *device)
1280 {
1281     WINMM_CBInfo cb_info;
1282     HRESULT hr;
1283     UINT32 pad, bufsize, avail_frames, queue_frames, written, ofs;
1284     UINT32 queue_bytes, nloops;
1285     BYTE *data;
1286     WAVEHDR *queue, *first = NULL;
1287
1288     TRACE("(%p)\n", device->handle);
1289
1290     EnterCriticalSection(&device->lock);
1291
1292     if(!device->device)
1293         goto exit;
1294
1295     if(!device->first){
1296         device->stopped = TRUE;
1297         device->last_clock_pos = 0;
1298         IAudioClient_Stop(device->client);
1299         IAudioClient_Reset(device->client);
1300         goto exit;
1301     }
1302
1303     hr = IAudioClient_GetBufferSize(device->client, &bufsize);
1304     if(FAILED(hr)){
1305         ERR("GetBufferSize failed: %08x\n", hr);
1306         goto exit;
1307     }
1308
1309     hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1310     if(FAILED(hr)){
1311         ERR("GetCurrentPadding failed: %08x\n", hr);
1312         goto exit;
1313     }
1314
1315     first = WOD_MarkDoneHeaders(device);
1316
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;
1320
1321     queue = device->playing;
1322     ofs = device->ofs_bytes;
1323     queue_frames = 0;
1324     nloops = 0;
1325     while(queue && queue_frames < avail_frames){
1326         queue_bytes = WINMM_HeaderLenBytes(device, queue);
1327         queue_frames += (queue_bytes - ofs) / device->bytes_per_frame;
1328         ofs = 0;
1329
1330         if(queue->dwFlags & WHDR_ENDLOOP && nloops < device->loop_counter){
1331             queue = device->loop_start;
1332             ++nloops;
1333         }else
1334             queue = queue->lpNext;
1335     }
1336
1337     if(avail_frames != 0 && queue_frames == 0){
1338         hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1339         if(FAILED(hr)){
1340             ERR("GetBuffer failed: %08x\n", hr);
1341             goto exit;
1342         }
1343
1344         hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames,
1345                 AUDCLNT_BUFFERFLAGS_SILENT);
1346         if(FAILED(hr)){
1347             ERR("ReleaseBuffer failed: %08x\n", hr);
1348             goto exit;
1349         }
1350
1351         goto exit;
1352     }
1353
1354     if(queue_frames < avail_frames)
1355         avail_frames = queue_frames;
1356     if(avail_frames == 0)
1357         goto exit;
1358
1359     hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1360     if(FAILED(hr)){
1361         ERR("GetBuffer failed: %08x\n", hr);
1362         goto exit;
1363     }
1364
1365     written = 0;
1366     while(device->playing && written < avail_frames){
1367         UINT32 copy_frames, copy_bytes;
1368         BYTE *queue_data;
1369
1370         queue = device->playing;
1371
1372         if(device->acm_handle){
1373             ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)queue->reserved;
1374             queue_bytes = ash->cbDstLengthUsed;
1375             queue_data = ash->pbDst;
1376         }else{
1377             queue_bytes = queue->dwBufferLength;
1378             queue_data = (BYTE*)queue->lpData;
1379         }
1380
1381         queue_frames = (queue_bytes - device->ofs_bytes) /
1382             device->bytes_per_frame;
1383
1384         copy_frames = queue_frames < (avail_frames - written) ?
1385             queue_frames : avail_frames - written;
1386         copy_bytes = copy_frames * device->bytes_per_frame;
1387
1388         memcpy(data, queue_data + device->ofs_bytes, copy_bytes);
1389
1390         data += copy_bytes;
1391         written += copy_frames;
1392         device->ofs_bytes += copy_bytes;
1393
1394         if(device->ofs_bytes >= queue_bytes){
1395             device->ofs_bytes = 0;
1396
1397             if(!(queue->dwFlags & (WHDR_BEGINLOOP | WHDR_ENDLOOP)))
1398                 device->playing = queue->lpNext;
1399             else{
1400                 if(queue->dwFlags & WHDR_BEGINLOOP){
1401                     device->loop_start = device->playing;
1402                     device->playing = queue->lpNext;
1403                     device->loop_counter = queue->dwLoops;
1404                 }
1405                 if(queue->dwFlags & WHDR_ENDLOOP){
1406                     --device->loop_counter;
1407                     if(device->loop_counter)
1408                         device->playing = device->loop_start;
1409                     else
1410                         device->loop_start = device->playing = queue->lpNext;
1411                 }
1412             }
1413         }
1414     }
1415
1416     hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames, 0);
1417     if(FAILED(hr)){
1418         ERR("ReleaseBuffer failed: %08x\n", hr);
1419         goto exit;
1420     }
1421
1422     device->played_frames += avail_frames;
1423
1424 exit:
1425     cb_info = device->cb_info;
1426
1427     LeaveCriticalSection(&device->lock);
1428
1429     while(first && (first->dwFlags & WHDR_DONE)){
1430         WAVEHDR *next = first->lpNext;
1431         WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1432         first = next;
1433     }
1434 }
1435
1436 static void WID_PullACMData(WINMM_Device *device)
1437 {
1438     UINT32 packet, packet_bytes;
1439     DWORD flags;
1440     BYTE *data;
1441     WAVEHDR *queue;
1442     HRESULT hr;
1443     MMRESULT mr;
1444
1445     if(device->acm_hdr.cbDstLength == 0){
1446         hr = IAudioClient_GetCurrentPadding(device->client, &packet);
1447         if(FAILED(hr)){
1448             ERR("GetCurrentPadding failed: %08x\n", hr);
1449             return;
1450         }
1451
1452         if(packet == 0)
1453             return;
1454
1455         hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1456                 &flags, NULL, NULL);
1457         if(FAILED(hr)){
1458             ERR("GetBuffer failed: %08x\n", hr);
1459             return;
1460         }
1461
1462         acmStreamSize(device->acm_handle, packet * device->bytes_per_frame,
1463                 &packet_bytes, ACM_STREAMSIZEF_SOURCE);
1464
1465         device->acm_offs = 0;
1466
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;
1478
1479         mr = acmStreamPrepareHeader(device->acm_handle, &device->acm_hdr, 0);
1480         if(mr != MMSYSERR_NOERROR){
1481             ERR("acmStreamPrepareHeader failed: %d\n", mr);
1482             return;
1483         }
1484
1485         mr = acmStreamConvert(device->acm_handle, &device->acm_hdr, 0);
1486         if(mr != MMSYSERR_NOERROR){
1487             ERR("acmStreamConvert failed: %d\n", mr);
1488             return;
1489         }
1490
1491         hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet);
1492         if(FAILED(hr))
1493             ERR("ReleaseBuffer failed: %08x\n", hr);
1494     }
1495
1496     queue = device->first;
1497     while(queue){
1498         UINT32 to_copy_bytes;
1499
1500         to_copy_bytes = min(queue->dwBufferLength - queue->dwBytesRecorded,
1501                 device->acm_hdr.cbDstLengthUsed - device->acm_offs);
1502
1503         memcpy(queue->lpData + queue->dwBytesRecorded,
1504                 device->acm_hdr.pbDst + device->acm_offs, to_copy_bytes);
1505
1506         queue->dwBytesRecorded += to_copy_bytes;
1507         device->acm_offs += to_copy_bytes;
1508
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;
1514         }
1515
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;
1521
1522             /* done with this ACM Header, so try to pull more data */
1523             WID_PullACMData(device);
1524             return;
1525         }
1526     }
1527
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;
1533 }
1534
1535 static void WID_PullData(WINMM_Device *device)
1536 {
1537     WINMM_CBInfo cb_info;
1538     WAVEHDR *queue, *first = NULL;
1539     HRESULT hr;
1540
1541     TRACE("(%p)\n", device->handle);
1542
1543     EnterCriticalSection(&device->lock);
1544
1545     if(!device->device || !device->first)
1546         goto exit;
1547
1548     first = device->first;
1549
1550     if(device->acm_handle){
1551         WID_PullACMData(device);
1552         goto exit;
1553     }
1554
1555     while(device->first){
1556         BYTE *data;
1557         UINT32 pad, packet_len, packet;
1558         DWORD flags;
1559
1560         hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1561         if(FAILED(hr)){
1562             ERR("GetCurrentPadding failed: %08x\n", hr);
1563             goto exit;
1564         }
1565
1566         if(pad == 0)
1567             goto exit;
1568
1569         hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1570                 &flags, NULL, NULL);
1571         if(FAILED(hr)){
1572             ERR("GetBuffer failed: %08x\n", hr);
1573             goto exit;
1574         }
1575
1576         packet_len = packet;
1577         queue = device->first;
1578         while(queue && packet > 0){
1579             UINT32 to_copy_bytes;
1580
1581             to_copy_bytes = min(packet * device->bytes_per_frame,
1582                     queue->dwBufferLength - queue->dwBytesRecorded);
1583
1584             memcpy(queue->lpData + queue->dwBytesRecorded, data,
1585                     to_copy_bytes);
1586
1587             queue->dwBytesRecorded += to_copy_bytes;
1588
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;
1594             }
1595
1596             packet -= to_copy_bytes / device->bytes_per_frame;
1597         }
1598
1599         hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_len);
1600         if(FAILED(hr))
1601             ERR("ReleaseBuffer failed: %08x\n", hr);
1602     }
1603
1604 exit:
1605     cb_info = device->cb_info;
1606
1607     LeaveCriticalSection(&device->lock);
1608
1609     while(first && (first->dwFlags & WHDR_DONE)){
1610         WAVEHDR *next = first->lpNext;
1611         WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1612         first = next;
1613     }
1614 }
1615
1616 static HRESULT WINMM_BeginPlaying(WINMM_Device *device)
1617 {
1618     HRESULT hr;
1619
1620     TRACE("(%p)\n", device->handle);
1621
1622     if(device->render)
1623         /* prebuffer data before starting */
1624         WOD_PushData(device);
1625
1626     if(device->stopped){
1627         device->stopped = FALSE;
1628
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);
1633             return hr;
1634         }
1635     }
1636
1637     return S_OK;
1638 }
1639
1640 static LRESULT WINMM_Pause(HWAVE hwave)
1641 {
1642     WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1643     HRESULT hr;
1644
1645     TRACE("(%p)\n", hwave);
1646
1647     if(!WINMM_ValidateAndLock(device))
1648         return MMSYSERR_INVALHANDLE;
1649
1650     hr = IAudioClient_Stop(device->client);
1651     if(FAILED(hr)){
1652         LeaveCriticalSection(&device->lock);
1653         ERR("Stop failed: %08x\n", hr);
1654         return MMSYSERR_ERROR;
1655     }
1656
1657     device->stopped = FALSE;
1658
1659     LeaveCriticalSection(&device->lock);
1660
1661     return MMSYSERR_NOERROR;
1662 }
1663
1664 static LRESULT WINMM_Reset(HWAVE hwave)
1665 {
1666     WINMM_CBInfo cb_info;
1667     WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1668     BOOL is_out;
1669     WAVEHDR *first;
1670     HRESULT hr;
1671
1672     TRACE("(%p)\n", hwave);
1673
1674     if(!WINMM_ValidateAndLock(device))
1675         return MMSYSERR_INVALHANDLE;
1676
1677     hr = IAudioClient_Stop(device->client);
1678     if(FAILED(hr)){
1679         LeaveCriticalSection(&device->lock);
1680         ERR("Stop failed: %08x\n", hr);
1681         return MMSYSERR_ERROR;
1682     }
1683     device->stopped = TRUE;
1684
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);
1692
1693     cb_info = device->cb_info;
1694     is_out = device->render ? TRUE : FALSE;
1695
1696     LeaveCriticalSection(&device->lock);
1697
1698     while(first){
1699         WAVEHDR *next = first->lpNext;
1700         first->dwFlags &= ~WHDR_INQUEUE;
1701         first->dwFlags |= WHDR_DONE;
1702         if(is_out)
1703             WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1704         else
1705             WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1706         first = next;
1707     }
1708
1709     return MMSYSERR_NOERROR;
1710 }
1711
1712 static MMRESULT WINMM_FramesToMMTime(MMTIME *time, UINT32 played_frames,
1713         UINT32 sample_rate, UINT32 bytes_per_frame)
1714 {
1715     switch(time->wType){
1716     case TIME_SAMPLES:
1717         time->u.sample = played_frames;
1718         return MMSYSERR_NOERROR;
1719     case TIME_MS:
1720         time->u.ms = (DWORD)((played_frames / (double)sample_rate) * 1000);
1721         return MMSYSERR_NOERROR;
1722     case TIME_SMPTE:
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;
1731         }else{
1732             time->u.smpte.sec = 0;
1733             time->u.smpte.min = 0;
1734             time->u.smpte.hour = 0;
1735         }
1736         time->u.smpte.frame = (played_frames / (double)sample_rate) * 30;
1737         return MMSYSERR_NOERROR;
1738     case TIME_BYTES:
1739     default:
1740         time->wType = TIME_BYTES;
1741         time->u.cb = played_frames * bytes_per_frame;
1742         return MMSYSERR_NOERROR;
1743     }
1744
1745     return MMSYSERR_ERROR;
1746 }
1747
1748 static LRESULT WINMM_GetPosition(HWAVE hwave, MMTIME *time)
1749 {
1750     WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1751     UINT32 played_frames, sample_rate, bytes_per_frame;
1752
1753     TRACE("(%p, %p)\n", hwave, time);
1754
1755     if(!WINMM_ValidateAndLock(device))
1756         return MMSYSERR_INVALHANDLE;
1757
1758     played_frames = device->played_frames;
1759     sample_rate = device->samples_per_sec;
1760     bytes_per_frame = device->bytes_per_frame;
1761
1762     LeaveCriticalSection(&device->lock);
1763
1764     return WINMM_FramesToMMTime(time, played_frames, sample_rate,
1765             bytes_per_frame);
1766 }
1767
1768 static WINMM_MMDevice *WINMM_GetMixerMMDevice(HMIXEROBJ hmix, DWORD flags,
1769         UINT *mmdev_index)
1770 {
1771     UINT mmdev, dev, junk, *out;
1772     BOOL is_out;
1773
1774     if(!mmdev_index)
1775         out = &mmdev;
1776     else
1777         out = mmdev_index;
1778
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];
1787         }
1788         /* fall through -- if it's not a valid mixer device, then
1789          * it could be a valid mixer handle. windows seems to do
1790          * this as well. */
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))
1797             return NULL;
1798         if(is_out)
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];
1805         return NULL;
1806     case MIXER_OBJECTF_WAVEIN:
1807         *out = HandleToULong(hmix);
1808         if(*out < g_inmmdevices_count)
1809             return &g_in_mmdevices[*out];
1810         return NULL;
1811     }
1812
1813     return NULL;
1814 }
1815
1816 static MMRESULT WINMM_SetupMMDeviceVolume(WINMM_MMDevice *mmdevice)
1817 {
1818     IAudioSessionManager *sesman;
1819     IMMDevice *device;
1820     HRESULT hr;
1821
1822     hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id, &device);
1823     if(FAILED(hr)){
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;
1828     }
1829
1830     hr = IMMDevice_Activate(device, &IID_IAudioSessionManager,
1831             CLSCTX_INPROC_SERVER, NULL, (void**)&sesman);
1832     if(FAILED(hr)){
1833         ERR("Activate failed: %08x\n", hr);
1834         IMMDevice_Release(device);
1835         return MMSYSERR_ERROR;
1836     }
1837
1838     IMMDevice_Release(device);
1839
1840     hr = IAudioSessionManager_GetSimpleAudioVolume(sesman, &mmdevice->session,
1841             FALSE, &mmdevice->volume);
1842     IAudioSessionManager_Release(sesman);
1843     if(FAILED(hr)){
1844         ERR("GetSimpleAudioVolume failed: %08x\n", hr);
1845         return MMSYSERR_ERROR;
1846     }
1847
1848     return MMSYSERR_NOERROR;
1849 }
1850
1851 static LRESULT MXD_GetControlDetails(WINMM_ControlDetails *details)
1852 {
1853     WINMM_MMDevice *mmdevice;
1854     MIXERCONTROLDETAILS *control = details->details;
1855     HRESULT hr;
1856
1857     TRACE("(%p)\n", details->hmix);
1858
1859     mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
1860     if(!mmdevice)
1861         return MMSYSERR_INVALHANDLE;
1862
1863     EnterCriticalSection(&mmdevice->lock);
1864
1865     if(!mmdevice->volume){
1866         MMRESULT mr;
1867
1868         mr = WINMM_SetupMMDeviceVolume(mmdevice);
1869         if(mr != MMSYSERR_NOERROR){
1870             LeaveCriticalSection(&mmdevice->lock);
1871             return mr;
1872         }
1873     }
1874
1875     if(control->dwControlID == 0){
1876         float vol;
1877         MIXERCONTROLDETAILS_UNSIGNED *udet;
1878
1879         if(!control->paDetails ||
1880                 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
1881             LeaveCriticalSection(&mmdevice->lock);
1882             return MMSYSERR_INVALPARAM;
1883         }
1884
1885         hr = ISimpleAudioVolume_GetMasterVolume(mmdevice->volume, &vol);
1886         if(FAILED(hr)){
1887             ERR("GetMasterVolume failed: %08x\n", hr);
1888             LeaveCriticalSection(&mmdevice->lock);
1889             return MMSYSERR_ERROR;
1890         }
1891
1892         udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
1893         udet->dwValue = vol * ((unsigned int)0xFFFF);
1894     }else if(control->dwControlID == 1){
1895         BOOL mute;
1896         MIXERCONTROLDETAILS_BOOLEAN *bdet;
1897
1898         if(!control->paDetails ||
1899                 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
1900             LeaveCriticalSection(&mmdevice->lock);
1901             return MMSYSERR_INVALPARAM;
1902         }
1903
1904         hr = ISimpleAudioVolume_GetMute(mmdevice->volume, &mute);
1905         if(FAILED(hr)){
1906             ERR("GetMute failed: %08x\n", hr);
1907             LeaveCriticalSection(&mmdevice->lock);
1908             return MMSYSERR_ERROR;
1909         }
1910
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");
1915     }else{
1916         LeaveCriticalSection(&mmdevice->lock);
1917         return MIXERR_INVALCONTROL;
1918     }
1919
1920     LeaveCriticalSection(&mmdevice->lock);
1921
1922     return MMSYSERR_NOERROR;
1923 }
1924
1925 static LRESULT MXD_SetControlDetails(WINMM_ControlDetails *details)
1926 {
1927     WINMM_MMDevice *mmdevice;
1928     MIXERCONTROLDETAILS *control = details->details;
1929     HRESULT hr;
1930
1931     TRACE("(%p)\n", details->hmix);
1932
1933     mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
1934     if(!mmdevice)
1935         return MMSYSERR_INVALHANDLE;
1936
1937     EnterCriticalSection(&mmdevice->lock);
1938
1939     if(!mmdevice->volume){
1940         MMRESULT mr;
1941
1942         mr = WINMM_SetupMMDeviceVolume(mmdevice);
1943         if(mr != MMSYSERR_NOERROR){
1944             LeaveCriticalSection(&mmdevice->lock);
1945             return mr;
1946         }
1947     }
1948
1949     if(control->dwControlID == 0){
1950         float vol;
1951         MIXERCONTROLDETAILS_UNSIGNED *udet;
1952
1953         if(!control->paDetails ||
1954                 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
1955             LeaveCriticalSection(&mmdevice->lock);
1956             return MMSYSERR_INVALPARAM;
1957         }
1958
1959         udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
1960
1961         if(udet->dwValue > 65535){
1962             LeaveCriticalSection(&mmdevice->lock);
1963             return MMSYSERR_INVALPARAM;
1964         }
1965
1966         vol = udet->dwValue / 65535.f;
1967
1968         hr = ISimpleAudioVolume_SetMasterVolume(mmdevice->volume, vol, NULL);
1969         if(FAILED(hr)){
1970             ERR("SetMasterVolume failed: %08x\n", hr);
1971             LeaveCriticalSection(&mmdevice->lock);
1972             return MMSYSERR_ERROR;
1973         }
1974     }else if(control->dwControlID == 1){
1975         BOOL mute;
1976         MIXERCONTROLDETAILS_BOOLEAN *bdet;
1977
1978         if(!control->paDetails ||
1979                 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
1980             LeaveCriticalSection(&mmdevice->lock);
1981             return MMSYSERR_INVALPARAM;
1982         }
1983
1984         bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
1985         mute = bdet->fValue;
1986
1987         hr = ISimpleAudioVolume_SetMute(mmdevice->volume, mute, NULL);
1988         if(FAILED(hr)){
1989             ERR("SetMute failed: %08x\n", hr);
1990             LeaveCriticalSection(&mmdevice->lock);
1991             return MMSYSERR_ERROR;
1992         }
1993     }else if(control->dwControlID == 2 || control->dwControlID == 3){
1994         FIXME("What should the sw-side mixer controls map to?\n");
1995     }else{
1996         LeaveCriticalSection(&mmdevice->lock);
1997         return MIXERR_INVALCONTROL;
1998     }
1999
2000     LeaveCriticalSection(&mmdevice->lock);
2001
2002     return MMSYSERR_NOERROR;
2003 }
2004
2005 static LRESULT CALLBACK WINMM_DevicesMsgProc(HWND hwnd, UINT msg, WPARAM wparam,
2006         LPARAM lparam)
2007 {
2008     switch(msg){
2009     case WODM_OPEN:
2010         return WOD_Open((WINMM_OpenInfo*)wparam);
2011     case WODM_CLOSE:
2012         return WOD_Close((HWAVEOUT)wparam);
2013     case WIDM_OPEN:
2014         return WID_Open((WINMM_OpenInfo*)wparam);
2015     case WIDM_CLOSE:
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);
2021     }
2022     return DefWindowProcW(hwnd, msg, wparam, lparam);
2023 }
2024
2025 static DWORD WINAPI WINMM_DevicesThreadProc(void *arg)
2026 {
2027     HANDLE evt = arg;
2028     HRESULT hr;
2029     static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
2030
2031     hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2032     if(FAILED(hr)){
2033         ERR("CoInitializeEx failed: %08x\n", hr);
2034         return 1;
2035     }
2036
2037     hr = WINMM_InitMMDevices();
2038     if(FAILED(hr)){
2039         CoUninitialize();
2040         return 1;
2041     }
2042
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());
2047         CoUninitialize();
2048         return 1;
2049     }
2050
2051     SetWindowLongPtrW(g_devices_hwnd, GWLP_WNDPROC,
2052             (LONG_PTR)WINMM_DevicesMsgProc);
2053
2054     /* inform caller that the thread is ready to process messages */
2055     SetEvent(evt);
2056     evt = NULL; /* do not use after this point */
2057
2058     while(1){
2059         DWORD wait;
2060         wait = MsgWaitForMultipleObjects(g_devhandle_count, g_device_handles,
2061                 FALSE, INFINITE, QS_ALLINPUT);
2062         if(wait == g_devhandle_count + WAIT_OBJECT_0){
2063             MSG msg;
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];
2068             if(device->render)
2069                 WOD_PushData(device);
2070             else
2071                 WID_PullData(device);
2072         }else
2073             ERR("Unexpected MsgWait result 0x%x, GLE: %d\n", wait,
2074                     GetLastError());
2075     }
2076
2077     DestroyWindow(g_devices_hwnd);
2078
2079     CoUninitialize();
2080
2081     return 0;
2082 }
2083
2084 static BOOL WINMM_StartDevicesThread(void)
2085 {
2086     HANDLE events[2];
2087     DWORD wait;
2088
2089     EnterCriticalSection(&g_devthread_lock);
2090
2091     if(g_devices_thread){
2092         DWORD wait;
2093
2094         wait = WaitForSingleObject(g_devices_thread, 0);
2095         if(wait == WAIT_TIMEOUT){
2096             LeaveCriticalSection(&g_devthread_lock);
2097             return TRUE;
2098         }
2099         if(wait != WAIT_OBJECT_0){
2100             LeaveCriticalSection(&g_devthread_lock);
2101             return FALSE;
2102         }
2103
2104         g_devices_thread = NULL;
2105         g_devices_hwnd = NULL;
2106     }
2107
2108     TRACE("Starting up devices thread\n");
2109
2110     events[0] = CreateEventW(NULL, FALSE, FALSE, NULL);
2111
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]);
2117         return FALSE;
2118     }
2119
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;
2128         }
2129         LeaveCriticalSection(&g_devthread_lock);
2130         return FALSE;
2131     }
2132
2133     LeaveCriticalSection(&g_devthread_lock);
2134
2135     return TRUE;
2136 }
2137
2138 /**************************************************************************
2139  *                              waveOutGetNumDevs               [WINMM.@]
2140  */
2141 UINT WINAPI waveOutGetNumDevs(void)
2142 {
2143     if(!WINMM_StartDevicesThread())
2144         return 0;
2145
2146     TRACE("count: %u\n", g_outmmdevices_count);
2147
2148     return g_outmmdevices_count;
2149 }
2150
2151 /**************************************************************************
2152  *                              waveOutGetDevCapsA              [WINMM.@]
2153  */
2154 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2155                                UINT uSize)
2156 {
2157     WAVEOUTCAPSW        wocW;
2158     UINT                ret;
2159
2160     TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2161
2162     if(!WINMM_StartDevicesThread())
2163         return MMSYSERR_ERROR;
2164
2165     if(!lpCaps)
2166         return MMSYSERR_INVALPARAM;
2167
2168     ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2169
2170     if (ret == MMSYSERR_NOERROR) {
2171         WAVEOUTCAPSA wocA;
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)));
2181     }
2182     return ret;
2183 }
2184
2185 /**************************************************************************
2186  *                              waveOutGetDevCapsW              [WINMM.@]
2187  */
2188 UINT WINAPI waveOutGetDevCapsW(UINT_PTR uDeviceID, LPWAVEOUTCAPSW lpCaps,
2189                                UINT uSize)
2190 {
2191     WAVEOUTCAPSW mapper_caps, *caps;
2192
2193     TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2194
2195     if(!WINMM_StartDevicesThread())
2196         return MMSYSERR_ERROR;
2197
2198     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2199
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};
2204
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);
2214
2215         caps = &mapper_caps;
2216     }else{
2217         if(uDeviceID >= g_outmmdevices_count)
2218             return MMSYSERR_BADDEVICEID;
2219
2220         caps = &g_out_mmdevices[uDeviceID].out_caps;
2221     }
2222
2223     memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2224
2225     return MMSYSERR_NOERROR;
2226 }
2227
2228 /**************************************************************************
2229  *                              waveOutGetErrorTextA    [WINMM.@]
2230  *                              waveInGetErrorTextA     [WINMM.@]
2231  */
2232 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2233 {
2234     UINT        ret;
2235
2236     if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2237     else if (uSize == 0) ret = MMSYSERR_NOERROR;
2238     else
2239     {
2240         LPWSTR  xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2241         if (!xstr) ret = MMSYSERR_NOMEM;
2242         else
2243         {
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);
2248         }
2249     }
2250     return ret;
2251 }
2252
2253 /**************************************************************************
2254  *                              waveOutGetErrorTextW    [WINMM.@]
2255  *                              waveInGetErrorTextW     [WINMM.@]
2256  */
2257 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2258 {
2259     UINT        ret = MMSYSERR_BADERRNUM;
2260
2261     if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2262     else if (uSize == 0) ret = MMSYSERR_NOERROR;
2263     else if (
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;
2271         }
2272     }
2273     return ret;
2274 }
2275
2276 /**************************************************************************
2277  *                      waveOutOpen                     [WINMM.@]
2278  * All the args/structs have the same layout as the win16 equivalents
2279  */
2280 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2281                        LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2282                        DWORD_PTR dwInstance, DWORD dwFlags)
2283 {
2284     LRESULT res;
2285     WINMM_OpenInfo info;
2286     WINMM_CBInfo cb_info;
2287
2288     TRACE("(%p, %u, %p, %lx, %lx, %08x)\n", lphWaveOut, uDeviceID, lpFormat,
2289             dwCallback, dwInstance, dwFlags);
2290
2291     if(!WINMM_StartDevicesThread())
2292         return MMSYSERR_ERROR;
2293
2294     if(!lphWaveOut && !(dwFlags & WAVE_FORMAT_QUERY))
2295         return MMSYSERR_INVALPARAM;
2296
2297     res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
2298     if(res != MMSYSERR_NOERROR)
2299         return res;
2300
2301     info.format = (WAVEFORMATEX*)lpFormat;
2302     info.callback = dwCallback;
2303     info.cb_user = dwInstance;
2304     info.req_device = uDeviceID;
2305     info.flags = dwFlags;
2306
2307     res = SendMessageW(g_devices_hwnd, WODM_OPEN, (DWORD_PTR)&info, 0);
2308     if(res != MMSYSERR_NOERROR)
2309         return res;
2310
2311     if(lphWaveOut)
2312         *lphWaveOut = (HWAVEOUT)info.handle;
2313
2314     cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2315     cb_info.callback = dwCallback;
2316     cb_info.user = dwInstance;
2317     cb_info.hwave = info.handle;
2318
2319     WINMM_NotifyClient(&cb_info, WOM_OPEN, 0, 0);
2320
2321     return res;
2322 }
2323
2324 /**************************************************************************
2325  *                              waveOutClose            [WINMM.@]
2326  */
2327 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2328 {
2329     UINT res;
2330     WINMM_Device *device;
2331     WINMM_CBInfo cb_info;
2332
2333     TRACE("(%p)\n", hWaveOut);
2334
2335     device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2336
2337     if(!WINMM_ValidateAndLock(device))
2338         return MMSYSERR_INVALHANDLE;
2339
2340     cb_info = device->cb_info;
2341
2342     LeaveCriticalSection(&device->lock);
2343
2344     res = SendMessageW(g_devices_hwnd, WODM_CLOSE, (WPARAM)hWaveOut, 0);
2345
2346     if(res == MMSYSERR_NOERROR)
2347         WINMM_NotifyClient(&cb_info, WOM_CLOSE, 0, 0);
2348
2349     return res;
2350 }
2351
2352 /**************************************************************************
2353  *                              waveOutPrepareHeader    [WINMM.@]
2354  */
2355 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2356                                  WAVEHDR* lpWaveOutHdr, UINT uSize)
2357 {
2358     TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2359
2360     if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2361         return MMSYSERR_INVALPARAM;
2362
2363     if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2364         return WAVERR_STILLPLAYING;
2365
2366     return WINMM_PrepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2367 }
2368
2369 /**************************************************************************
2370  *                              waveOutUnprepareHeader  [WINMM.@]
2371  */
2372 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2373                                    LPWAVEHDR lpWaveOutHdr, UINT uSize)
2374 {
2375     TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2376
2377     if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2378         return MMSYSERR_INVALPARAM;
2379     
2380     if(!(lpWaveOutHdr->dwFlags & WHDR_PREPARED))
2381         return MMSYSERR_NOERROR;
2382
2383     if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2384         return WAVERR_STILLPLAYING;
2385
2386     return WINMM_UnprepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2387 }
2388
2389 /**************************************************************************
2390  *                              waveOutWrite            [WINMM.@]
2391  */
2392 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, WAVEHDR *header, UINT uSize)
2393 {
2394     WINMM_Device *device;
2395     HRESULT hr;
2396
2397     TRACE("(%p, %p, %u)\n", hWaveOut, header, uSize);
2398
2399     device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2400
2401     if(!WINMM_ValidateAndLock(device))
2402         return MMSYSERR_INVALHANDLE;
2403
2404     if(!header->lpData || !(header->dwFlags & WHDR_PREPARED)){
2405         LeaveCriticalSection(&device->lock);
2406         return WAVERR_UNPREPARED;
2407     }
2408
2409     if(header->dwFlags & WHDR_INQUEUE){
2410         LeaveCriticalSection(&device->lock);
2411         return WAVERR_STILLPLAYING;
2412     }
2413
2414     if(device->acm_handle){
2415         ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
2416         MMRESULT mr;
2417
2418         ash->cbSrcLength = header->dwBufferLength;
2419         mr = acmStreamConvert(device->acm_handle, ash, 0);
2420         if(mr != MMSYSERR_NOERROR){
2421             LeaveCriticalSection(&device->lock);
2422             return mr;
2423         }
2424     }
2425
2426     if(device->first){
2427         device->last->lpNext = header;
2428         device->last = header;
2429         if(!device->playing)
2430             device->playing = header;
2431     }else{
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;
2436         }
2437     }
2438
2439     header->lpNext = NULL;
2440     header->dwFlags &= ~WHDR_DONE;
2441     header->dwFlags |= WHDR_INQUEUE;
2442
2443     hr = WINMM_BeginPlaying(device);
2444     if(FAILED(hr)){
2445         LeaveCriticalSection(&device->lock);
2446         return MMSYSERR_ERROR;
2447     }
2448
2449     LeaveCriticalSection(&device->lock);
2450
2451     return MMSYSERR_NOERROR;
2452 }
2453
2454 /**************************************************************************
2455  *                              waveOutBreakLoop        [WINMM.@]
2456  */
2457 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2458 {
2459     WINMM_Device *device;
2460
2461     TRACE("(%p)\n", hWaveOut);
2462
2463     device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2464
2465     if(!WINMM_ValidateAndLock(device))
2466         return MMSYSERR_INVALHANDLE;
2467
2468     device->loop_counter = 0;
2469
2470     LeaveCriticalSection(&device->lock);
2471
2472     return MMSYSERR_NOERROR;
2473 }
2474
2475 /**************************************************************************
2476  *                              waveOutPause            [WINMM.@]
2477  */
2478 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2479 {
2480     TRACE("(%p)\n", hWaveOut);
2481
2482     return WINMM_Pause((HWAVE)hWaveOut);
2483 }
2484
2485 /**************************************************************************
2486  *                              waveOutReset            [WINMM.@]
2487  */
2488 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2489 {
2490     TRACE("(%p)\n", hWaveOut);
2491
2492     return WINMM_Reset((HWAVE)hWaveOut);
2493 }
2494
2495 /**************************************************************************
2496  *                              waveOutRestart          [WINMM.@]
2497  */
2498 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2499 {
2500     WINMM_Device *device;
2501     HRESULT hr;
2502
2503     TRACE("(%p)\n", hWaveOut);
2504
2505     device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2506
2507     if(!WINMM_ValidateAndLock(device))
2508         return MMSYSERR_INVALHANDLE;
2509
2510     device->stopped = TRUE;
2511
2512     hr = WINMM_BeginPlaying(device);
2513     if(FAILED(hr)){
2514         LeaveCriticalSection(&device->lock);
2515         return MMSYSERR_ERROR;
2516     }
2517
2518     LeaveCriticalSection(&device->lock);
2519
2520     return MMSYSERR_NOERROR;
2521 }
2522
2523 /**************************************************************************
2524  *                              waveOutGetPosition      [WINMM.@]
2525  */
2526 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2527                                UINT uSize)
2528 {
2529     TRACE("(%p, %p, %u)\n", hWaveOut, lpTime, uSize);
2530
2531     if(!uSize || !lpTime || uSize != sizeof(MMTIME))
2532         return MMSYSERR_INVALPARAM;
2533
2534     return WINMM_GetPosition((HWAVE)hWaveOut, lpTime);
2535 }
2536
2537 /**************************************************************************
2538  *                              waveOutGetPitch         [WINMM.@]
2539  */
2540 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
2541 {
2542     TRACE("(%p, %p)\n", hWaveOut, lpdw);
2543     return MMSYSERR_NOTSUPPORTED;
2544 }
2545
2546 /**************************************************************************
2547  *                              waveOutSetPitch         [WINMM.@]
2548  */
2549 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
2550 {
2551     TRACE("(%p, %08x)\n", hWaveOut, dw);
2552
2553     return MMSYSERR_NOTSUPPORTED;
2554 }
2555
2556 /**************************************************************************
2557  *                              waveOutGetPlaybackRate  [WINMM.@]
2558  */
2559 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
2560 {
2561     TRACE("(%p, %p)\n", hWaveOut, lpdw);
2562
2563     return MMSYSERR_NOTSUPPORTED;
2564 }
2565
2566 /**************************************************************************
2567  *                              waveOutSetPlaybackRate  [WINMM.@]
2568  */
2569 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
2570 {
2571     TRACE("(%p, %08x)\n", hWaveOut, dw);
2572
2573     return MMSYSERR_NOTSUPPORTED;
2574 }
2575
2576 /**************************************************************************
2577  *                              waveOutGetVolume        [WINMM.@]
2578  */
2579 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, DWORD *out)
2580 {
2581     WINMM_Device *device;
2582     UINT32 channels;
2583     float *vols;
2584     HRESULT hr;
2585
2586     TRACE("(%p, %p)\n", hWaveOut, out);
2587
2588     if(!out)
2589         return MMSYSERR_INVALPARAM;
2590
2591     device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2592
2593     if(!WINMM_ValidateAndLock(device))
2594         return MMSYSERR_INVALHANDLE;
2595
2596     hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
2597     if(FAILED(hr)){
2598         LeaveCriticalSection(&device->lock);
2599         ERR("GetChannelCount failed: %08x\n", hr);
2600         return MMSYSERR_ERROR;
2601     }
2602
2603     vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
2604     if(!vols){
2605         LeaveCriticalSection(&device->lock);
2606         return MMSYSERR_NOMEM;
2607     }
2608
2609     hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
2610     if(FAILED(hr)){
2611         LeaveCriticalSection(&device->lock);
2612         HeapFree(GetProcessHeap(), 0, vols);
2613         ERR("GetAllVolumes failed: %08x\n", hr);
2614         return MMSYSERR_ERROR;
2615     }
2616
2617     LeaveCriticalSection(&device->lock);
2618
2619     *out = ((UINT16)(vols[0] * (DWORD)0xFFFF));
2620     if(channels > 1)
2621         *out |= ((UINT16)(vols[1] * (DWORD)0xFFFF)) << 16;
2622
2623     HeapFree(GetProcessHeap(), 0, vols);
2624
2625     return MMSYSERR_NOERROR;
2626 }
2627
2628 /**************************************************************************
2629  *                              waveOutSetVolume        [WINMM.@]
2630  */
2631 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD in)
2632 {
2633     WINMM_Device *device;
2634     UINT32 channels;
2635     float *vols;
2636     HRESULT hr;
2637
2638     TRACE("(%p, %08x)\n", hWaveOut, in);
2639
2640     device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2641
2642     if(!WINMM_ValidateAndLock(device))
2643         return MMSYSERR_INVALHANDLE;
2644
2645     hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
2646     if(FAILED(hr)){
2647         LeaveCriticalSection(&device->lock);
2648         ERR("GetChannelCount failed: %08x\n", hr);
2649         return MMSYSERR_ERROR;
2650     }
2651
2652     vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
2653     if(!vols){
2654         LeaveCriticalSection(&device->lock);
2655         return MMSYSERR_NOMEM;
2656     }
2657
2658     hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
2659     if(FAILED(hr)){
2660         LeaveCriticalSection(&device->lock);
2661         HeapFree(GetProcessHeap(), 0, vols);
2662         ERR("GetAllVolumes failed: %08x\n", hr);
2663         return MMSYSERR_ERROR;
2664     }
2665
2666     vols[0] = (float)((DWORD)(in & 0xFFFF) / (float)0xFFFF);
2667     if(channels > 1)
2668         vols[1] = (float)((DWORD)(in >> 16) / (float)0xFFFF);
2669
2670     hr = IAudioStreamVolume_SetAllVolumes(device->volume, channels, vols);
2671     if(FAILED(hr)){
2672         LeaveCriticalSection(&device->lock);
2673         HeapFree(GetProcessHeap(), 0, vols);
2674         ERR("SetAllVolumes failed: %08x\n", hr);
2675         return MMSYSERR_ERROR;
2676     }
2677
2678     LeaveCriticalSection(&device->lock);
2679
2680     HeapFree(GetProcessHeap(), 0, vols);
2681
2682     return MMSYSERR_NOERROR;
2683 }
2684
2685 /**************************************************************************
2686  *                              waveOutGetID            [WINMM.@]
2687  */
2688 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
2689 {
2690     WINMM_Device *device;
2691     UINT dev, junk;
2692     BOOL is_out;
2693
2694     TRACE("(%p, %p)\n", hWaveOut, lpuDeviceID);
2695
2696     if(!lpuDeviceID)
2697         return MMSYSERR_INVALPARAM;
2698
2699     device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2700     if(!WINMM_ValidateAndLock(device))
2701         return MMSYSERR_INVALHANDLE;
2702
2703     LeaveCriticalSection(&device->lock);
2704
2705     WINMM_DecomposeHWAVE((HWAVE)hWaveOut, lpuDeviceID, &is_out, &dev, &junk);
2706
2707     return MMSYSERR_NOERROR;
2708 }
2709
2710 static UINT WINMM_QueryInstanceIDSize(UINT device, DWORD_PTR *len, BOOL is_out)
2711 {
2712     UINT count;
2713     WINMM_MMDevice *devices;
2714
2715     TRACE("(%u, %p, %d)\n", device, len, is_out);
2716
2717     if(is_out){
2718         count = g_outmmdevices_count;
2719         devices = g_out_mmdevices;
2720     }else{
2721         count = g_inmmdevices_count;
2722         devices = g_in_mmdevices;
2723     }
2724
2725     if(device >= count)
2726         return MMSYSERR_INVALHANDLE;
2727
2728     *len = (lstrlenW(devices[device].dev_id) + 1) * sizeof(WCHAR);
2729
2730     return MMSYSERR_NOERROR;
2731 }
2732
2733 static UINT WINMM_QueryInstanceID(UINT device, WCHAR *str, DWORD_PTR len,
2734         BOOL is_out)
2735 {
2736     UINT count, id_len;
2737     WINMM_MMDevice *devices;
2738
2739     TRACE("(%u, %p, %d)\n", device, str, is_out);
2740
2741     if(is_out){
2742         count = g_outmmdevices_count;
2743         devices = g_out_mmdevices;
2744     }else{
2745         count = g_inmmdevices_count;
2746         devices = g_in_mmdevices;
2747     }
2748
2749     if(device >= count)
2750         return MMSYSERR_INVALHANDLE;
2751
2752     id_len = (lstrlenW(devices[device].dev_id) + 1) * sizeof(WCHAR);
2753     if(len < id_len)
2754         return MMSYSERR_ERROR;
2755
2756     memcpy(str, devices[device].dev_id, id_len);
2757
2758     return MMSYSERR_NOERROR;
2759 }
2760
2761 /**************************************************************************
2762  *                              waveOutMessage          [WINMM.@]
2763  */
2764 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
2765                            DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2766 {
2767     TRACE("(%p, %u, %lx, %lx)\n", hWaveOut, uMessage, dwParam1, dwParam2);
2768
2769     switch(uMessage){
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);
2775     }
2776
2777     return MMSYSERR_NOTSUPPORTED;
2778 }
2779
2780 /**************************************************************************
2781  *                              waveInGetNumDevs                [WINMM.@]
2782  */
2783 UINT WINAPI waveInGetNumDevs(void)
2784 {
2785     if(!WINMM_StartDevicesThread())
2786         return 0;
2787
2788     TRACE("count: %u\n", g_inmmdevices_count);
2789
2790     return g_inmmdevices_count;
2791 }
2792
2793 /**************************************************************************
2794  *                              waveInGetDevCapsW               [WINMM.@]
2795  */
2796 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
2797 {
2798     WAVEINCAPSW mapper_caps, *caps;
2799
2800     TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2801
2802     if(!WINMM_StartDevicesThread())
2803         return MMSYSERR_ERROR;
2804
2805     if(!lpCaps)
2806         return MMSYSERR_INVALPARAM;
2807
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};
2812
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);
2820
2821         caps = &mapper_caps;
2822     }else{
2823         if(uDeviceID >= g_inmmdevices_count)
2824             return MMSYSERR_BADDEVICEID;
2825
2826         caps = &g_in_mmdevices[uDeviceID].in_caps;
2827     }
2828
2829     memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2830
2831     return MMSYSERR_NOERROR;
2832 }
2833
2834 /**************************************************************************
2835  *                              waveInGetDevCapsA               [WINMM.@]
2836  */
2837 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
2838 {
2839     UINT ret;
2840     WAVEINCAPSW wicW;
2841
2842     TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2843
2844     if(!WINMM_StartDevicesThread())
2845         return MMSYSERR_ERROR;
2846
2847     if(!lpCaps)
2848         return MMSYSERR_INVALPARAM;
2849
2850     ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
2851
2852     if (ret == MMSYSERR_NOERROR) {
2853         WAVEINCAPSA wicA;
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)));
2862     }
2863     return ret;
2864 }
2865
2866 /**************************************************************************
2867  *                              waveInOpen                      [WINMM.@]
2868  */
2869 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
2870                            LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2871                            DWORD_PTR dwInstance, DWORD dwFlags)
2872 {
2873     LRESULT res;
2874     WINMM_OpenInfo info;
2875     WINMM_CBInfo cb_info;
2876
2877     TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn, uDeviceID, lpFormat,
2878             dwCallback, dwInstance, dwFlags);
2879
2880     if(!WINMM_StartDevicesThread())
2881         return MMSYSERR_ERROR;
2882
2883     if(!lphWaveIn && !(dwFlags & WAVE_FORMAT_QUERY))
2884         return MMSYSERR_INVALPARAM;
2885
2886     res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
2887     if(res != MMSYSERR_NOERROR)
2888         return res;
2889
2890     info.format = (WAVEFORMATEX*)lpFormat;
2891     info.callback = dwCallback;
2892     info.cb_user = dwInstance;
2893     info.req_device = uDeviceID;
2894     info.flags = dwFlags;
2895
2896     res = SendMessageW(g_devices_hwnd, WIDM_OPEN, (DWORD_PTR)&info, 0);
2897     if(res != MMSYSERR_NOERROR)
2898         return res;
2899
2900     if(lphWaveIn)
2901         *lphWaveIn = (HWAVEIN)info.handle;
2902
2903     cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2904     cb_info.callback = dwCallback;
2905     cb_info.user = dwInstance;
2906     cb_info.hwave = info.handle;
2907
2908     WINMM_NotifyClient(&cb_info, WIM_OPEN, 0, 0);
2909
2910     return res;
2911 }
2912
2913 /**************************************************************************
2914  *                              waveInClose                     [WINMM.@]
2915  */
2916 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
2917 {
2918     WINMM_Device *device;
2919     WINMM_CBInfo cb_info;
2920     UINT res;
2921
2922     TRACE("(%p)\n", hWaveIn);
2923
2924     device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
2925
2926     if(!WINMM_ValidateAndLock(device))
2927         return MMSYSERR_INVALHANDLE;
2928
2929     cb_info = device->cb_info;
2930
2931     LeaveCriticalSection(&device->lock);
2932
2933     res = SendMessageW(g_devices_hwnd, WIDM_CLOSE, (WPARAM)hWaveIn, 0);
2934
2935     if(res == MMSYSERR_NOERROR)
2936         WINMM_NotifyClient(&cb_info, WIM_CLOSE, 0, 0);
2937
2938     return res;
2939 }
2940
2941 /**************************************************************************
2942  *                              waveInPrepareHeader             [WINMM.@]
2943  */
2944 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
2945                                 UINT uSize)
2946 {
2947     TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
2948
2949     if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
2950         return MMSYSERR_INVALPARAM;
2951
2952     if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
2953         return WAVERR_STILLPLAYING;
2954
2955     return WINMM_PrepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
2956 }
2957
2958 /**************************************************************************
2959  *                              waveInUnprepareHeader   [WINMM.@]
2960  */
2961 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
2962                                   UINT uSize)
2963 {
2964     TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
2965
2966     if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
2967         return MMSYSERR_INVALPARAM;
2968
2969     if(!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
2970         return MMSYSERR_NOERROR;
2971
2972     if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
2973         return WAVERR_STILLPLAYING;
2974
2975     return WINMM_UnprepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
2976 }
2977
2978 /**************************************************************************
2979  *                              waveInAddBuffer         [WINMM.@]
2980  */
2981 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn, WAVEHDR *header, UINT uSize)
2982 {
2983     WINMM_Device *device;
2984
2985     TRACE("(%p, %p, %u)\n", hWaveIn, header, uSize);
2986
2987     if(!header || uSize < sizeof(WAVEHDR))
2988         return MMSYSERR_INVALPARAM;
2989
2990     if(!(header->dwFlags & WHDR_PREPARED))
2991         return WAVERR_UNPREPARED;
2992
2993     device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
2994
2995     if(!WINMM_ValidateAndLock(device))
2996         return MMSYSERR_INVALHANDLE;
2997
2998     if(!device->first)
2999         device->first = device->last = header;
3000     else{
3001         device->last->lpNext = header;
3002         device->last = header;
3003     }
3004
3005     header->dwBytesRecorded = 0;
3006     header->lpNext = NULL;
3007     header->dwFlags &= ~WHDR_DONE;
3008     header->dwFlags |= WHDR_INQUEUE;
3009
3010     LeaveCriticalSection(&device->lock);
3011
3012     return MMSYSERR_NOERROR;
3013 }
3014
3015 /**************************************************************************
3016  *                              waveInReset             [WINMM.@]
3017  */
3018 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
3019 {
3020     TRACE("(%p)\n", hWaveIn);
3021
3022     return WINMM_Reset((HWAVE)hWaveIn);
3023 }
3024
3025 /**************************************************************************
3026  *                              waveInStart             [WINMM.@]
3027  */
3028 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
3029 {
3030     WINMM_Device *device;
3031     HRESULT hr;
3032
3033     TRACE("(%p)\n", hWaveIn);
3034
3035     device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3036
3037     if(!WINMM_ValidateAndLock(device))
3038         return MMSYSERR_INVALHANDLE;
3039
3040     hr = WINMM_BeginPlaying(device);
3041     if(FAILED(hr)){
3042         LeaveCriticalSection(&device->lock);
3043         return MMSYSERR_ERROR;
3044     }
3045
3046     LeaveCriticalSection(&device->lock);
3047
3048     return MMSYSERR_NOERROR;
3049 }
3050
3051 /**************************************************************************
3052  *                              waveInStop              [WINMM.@]
3053  */
3054 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
3055 {
3056     WINMM_CBInfo cb_info;
3057     WINMM_Device *device;
3058     WAVEHDR *buf;
3059     HRESULT hr;
3060
3061     TRACE("(%p)\n", hWaveIn);
3062
3063     device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3064
3065     if(!WINMM_ValidateAndLock(device))
3066         return MMSYSERR_INVALHANDLE;
3067
3068     hr = WINMM_Pause((HWAVE)hWaveIn);
3069     if(FAILED(hr)){
3070         LeaveCriticalSection(&device->lock);
3071         return MMSYSERR_ERROR;
3072     }
3073     device->stopped = TRUE;
3074
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;
3080     }else
3081         buf = NULL;
3082
3083     cb_info = device->cb_info;
3084
3085     LeaveCriticalSection(&device->lock);
3086
3087     if(buf)
3088         WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)buf, 0);
3089
3090     return MMSYSERR_NOERROR;
3091 }
3092
3093 /**************************************************************************
3094  *                              waveInGetPosition       [WINMM.@]
3095  */
3096 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
3097                               UINT uSize)
3098 {
3099     TRACE("(%p, %p, %u)\n", hWaveIn, lpTime, uSize);
3100
3101     if(!uSize || !lpTime || uSize != sizeof(MMTIME))
3102         return MMSYSERR_INVALPARAM;
3103
3104     return WINMM_GetPosition((HWAVE)hWaveIn, lpTime);
3105 }
3106
3107 /**************************************************************************
3108  *                              waveInGetID                     [WINMM.@]
3109  */
3110 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
3111 {
3112     UINT dev, junk;
3113     BOOL is_out;
3114     WINMM_Device *device;
3115
3116     TRACE("(%p, %p)\n", hWaveIn, lpuDeviceID);
3117
3118     if(!lpuDeviceID)
3119         return MMSYSERR_INVALPARAM;
3120
3121     device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3122     if(!WINMM_ValidateAndLock(device))
3123         return MMSYSERR_INVALHANDLE;
3124
3125     LeaveCriticalSection(&device->lock);
3126
3127     WINMM_DecomposeHWAVE((HWAVE)hWaveIn, lpuDeviceID, &is_out, &dev, &junk);
3128
3129     return MMSYSERR_NOERROR;
3130 }
3131
3132 /**************************************************************************
3133  *                              waveInMessage           [WINMM.@]
3134  */
3135 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
3136                           DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3137 {
3138     TRACE("(%p, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
3139
3140     switch(uMessage){
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);
3146     }
3147
3148     return MMSYSERR_NOTSUPPORTED;
3149 }
3150
3151 UINT WINAPI mixerGetNumDevs(void)
3152 {
3153     TRACE("\n");
3154
3155     if(!WINMM_StartDevicesThread())
3156         return 0;
3157
3158     return g_outmmdevices_count + g_inmmdevices_count;
3159 }
3160
3161 /**************************************************************************
3162  *                              mixerGetDevCapsA                [WINMM.@]
3163  */
3164 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
3165 {
3166     MIXERCAPSW micW;
3167     UINT       ret;
3168
3169     TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3170
3171     if(!lpCaps)
3172         return MMSYSERR_INVALPARAM;
3173
3174     ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
3175
3176     if (ret == MMSYSERR_NOERROR) {
3177         MIXERCAPSA micA;
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)));
3186     }
3187     return ret;
3188 }
3189
3190 /**************************************************************************
3191  *                              mixerGetDevCapsW                [WINMM.@]
3192  */
3193 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
3194 {
3195     WINMM_MMDevice *mmdevice;
3196     MIXERCAPSW caps;
3197
3198     TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3199
3200     if(!WINMM_StartDevicesThread())
3201         return MMSYSERR_ERROR;
3202
3203     if(!lpCaps)
3204         return MMSYSERR_INVALPARAM;
3205
3206     if(!uSize)
3207         return MMSYSERR_NOERROR;
3208
3209     if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3210         return MMSYSERR_BADDEVICEID;
3211
3212     if(uDeviceID < g_outmmdevices_count){
3213         mmdevice = &g_out_mmdevices[uDeviceID];
3214         memcpy(caps.szPname, mmdevice->out_caps.szPname, sizeof(caps.szPname));
3215     }else{
3216         mmdevice = &g_in_mmdevices[uDeviceID - g_outmmdevices_count];
3217         memcpy(caps.szPname, mmdevice->in_caps.szPname, sizeof(caps.szPname));
3218     }
3219
3220     caps.wMid = 0xFF;
3221     caps.wPid = 0xFF;
3222     caps.vDriverVersion = 0x00010001;
3223     caps.fdwSupport = 0;
3224     caps.cDestinations = 1;
3225
3226     memcpy(lpCaps, &caps, uSize);
3227
3228     return MMSYSERR_NOERROR;
3229 }
3230
3231 /**************************************************************************
3232  *                              mixerOpen                       [WINMM.@]
3233  */
3234 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
3235                       DWORD_PTR dwInstance, DWORD fdwOpen)
3236 {
3237     WINMM_MMDevice *mmdevice;
3238     MMRESULT mr;
3239
3240     TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix, uDeviceID, dwCallback,
3241             dwInstance, fdwOpen);
3242
3243     if(!WINMM_StartDevicesThread())
3244         return MMSYSERR_ERROR;
3245
3246     if(!lphMix)
3247         return MMSYSERR_INVALPARAM;
3248
3249     mr = WINMM_CheckCallback(dwCallback, fdwOpen, TRUE);
3250     if(mr != MMSYSERR_NOERROR)
3251         return mr;
3252
3253     if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3254         return MMSYSERR_BADDEVICEID;
3255
3256     if(uDeviceID < g_outmmdevices_count){
3257         mmdevice = &g_out_mmdevices[uDeviceID];
3258         *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID, TRUE,
3259                 mmdevice->mixer_count);
3260     }else{
3261         mmdevice = &g_in_mmdevices[uDeviceID - g_outmmdevices_count];
3262         *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID - g_outmmdevices_count,
3263                 FALSE, mmdevice->mixer_count);
3264     }
3265
3266     ++mmdevice->mixer_count;
3267
3268     return MMSYSERR_NOERROR;
3269 }
3270
3271 /**************************************************************************
3272  *                              mixerClose                      [WINMM.@]
3273  */
3274 UINT WINAPI mixerClose(HMIXER hMix)
3275 {
3276     TRACE("(%p)\n", hMix);
3277
3278     return MMSYSERR_NOERROR;
3279 }
3280
3281 /**************************************************************************
3282  *                              mixerGetID                      [WINMM.@]
3283  */
3284 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
3285 {
3286     WINMM_MMDevice *mmdevice;
3287
3288     TRACE("(%p, %p, %x)\n", hmix, lpid, fdwID);
3289
3290     if(!WINMM_StartDevicesThread())
3291         return MMSYSERR_ERROR;
3292
3293     if(!lpid)
3294         return MMSYSERR_INVALPARAM;
3295
3296     mmdevice = WINMM_GetMixerMMDevice(hmix, fdwID, lpid);
3297     if(!mmdevice)
3298         return MMSYSERR_INVALHANDLE;
3299
3300     if(mmdevice->in_caps.szPname[0] != '\0')
3301         *lpid += g_outmmdevices_count;
3302
3303     return MMSYSERR_NOERROR;
3304 }
3305
3306 /**************************************************************************
3307  *                              mixerGetControlDetailsW         [WINMM.@]
3308  */
3309 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
3310                                     DWORD fdwDetails)
3311 {
3312     WINMM_ControlDetails details;
3313
3314     TRACE("(%p, %p, %x)\n", hmix, lpmcdW, fdwDetails);
3315
3316     if(!WINMM_StartDevicesThread())
3317         return MMSYSERR_ERROR;
3318
3319     if(!lpmcdW)
3320         return MMSYSERR_INVALPARAM;
3321
3322     TRACE("dwControlID: %u\n", lpmcdW->dwControlID);
3323
3324     details.hmix = hmix;
3325     details.details = lpmcdW;
3326     details.flags = fdwDetails;
3327
3328     return SendMessageW(g_devices_hwnd, MXDM_GETCONTROLDETAILS,
3329             (DWORD_PTR)&details, 0);
3330 }
3331
3332 /**************************************************************************
3333  *                              mixerGetControlDetailsA [WINMM.@]
3334  */
3335 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
3336                                     DWORD fdwDetails)
3337 {
3338     UINT ret = MMSYSERR_NOTSUPPORTED;
3339
3340     TRACE("(%p, %p, %08x)\n", hmix, lpmcdA, fdwDetails);
3341
3342     if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
3343         return MMSYSERR_INVALPARAM;
3344
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);
3349         break;
3350     case MIXER_GETCONTROLDETAILSF_LISTTEXT:
3351         {
3352             MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
3353             MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
3354             int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3355             unsigned int i;
3356
3357             if (lpmcdA->u.cMultipleItems != 0) {
3358                 size *= lpmcdA->u.cMultipleItems;
3359             }
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,
3371                                          pDetailsA->szName,
3372                                          sizeof(pDetailsA->szName), NULL, NULL );
3373                     pDetailsA++;
3374                     pDetailsW++;
3375                 }
3376                 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3377                 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3378             }
3379             HeapFree(GetProcessHeap(), 0, pDetailsW);
3380             lpmcdA->paDetails = pDetailsA;
3381             lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
3382         }
3383         break;
3384     default:
3385         ERR("Unsupported fdwDetails=0x%08x\n", fdwDetails);
3386     }
3387
3388     return ret;
3389 }
3390
3391 /**************************************************************************
3392  *                              mixerGetLineControlsA   [WINMM.@]
3393  */
3394 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
3395                                   DWORD fdwControls)
3396 {
3397     MIXERLINECONTROLSW  mlcW;
3398     DWORD               ret;
3399     unsigned int        i;
3400
3401     TRACE("(%p, %p, %x)\n", hmix, lpmlcA, fdwControls);
3402
3403     if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
3404         lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
3405         return MMSYSERR_INVALPARAM;
3406
3407     mlcW.cbStruct = sizeof(mlcW);
3408     mlcW.dwLineID = lpmlcA->dwLineID;
3409     mlcW.u.dwControlID = lpmlcA->u.dwControlID;
3410     mlcW.u.dwControlType = lpmlcA->u.dwControlType;
3411
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)) {
3416         mlcW.cControls = 1;
3417     } else {
3418         mlcW.cControls = lpmlcA->cControls;
3419     }
3420     mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
3421     mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
3422                               mlcW.cControls * mlcW.cbmxctrl);
3423
3424     ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
3425
3426     if (ret == MMSYSERR_NOERROR) {
3427         lpmlcA->dwLineID = mlcW.dwLineID;
3428         lpmlcA->u.dwControlID = mlcW.u.dwControlID;
3429         lpmlcA->u.dwControlType = mlcW.u.dwControlType;
3430
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));
3451         }
3452     }
3453
3454     HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
3455
3456     return ret;
3457 }
3458
3459 static UINT WINMM_GetVolumeLineControl(WINMM_MMDevice *mmdevice, DWORD line,
3460         MIXERCONTROLW *ctl, DWORD flags)
3461 {
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;
3471
3472     return MMSYSERR_NOERROR;
3473 }
3474
3475 static UINT WINMM_GetMuteLineControl(WINMM_MMDevice *mmdevice, DWORD line,
3476         MIXERCONTROLW *ctl, DWORD flags)
3477 {
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;
3487
3488     return MMSYSERR_NOERROR;
3489 }
3490
3491 /**************************************************************************
3492  *                              mixerGetLineControlsW           [WINMM.@]
3493  */
3494 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
3495                                   DWORD fdwControls)
3496 {
3497     WINMM_MMDevice *mmdevice;
3498
3499     TRACE("(%p, %p, %08x)\n", hmix, lpmlcW, fdwControls);
3500
3501     if(!WINMM_StartDevicesThread())
3502         return MMSYSERR_ERROR;
3503
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;
3511     }
3512
3513     if(!lpmlcW || lpmlcW->cbStruct < sizeof(*lpmlcW) || !lpmlcW->pamxctrl)
3514         return MMSYSERR_INVALPARAM;
3515
3516     TRACE("dwLineID: %u\n", lpmlcW->dwLineID);
3517     TRACE("dwControl: %x\n", lpmlcW->u.dwControlID);
3518     TRACE("cControls: %u\n", lpmlcW->cControls);
3519
3520     mmdevice = WINMM_GetMixerMMDevice(hmix, fdwControls, NULL);
3521     if(!mmdevice)
3522         return MMSYSERR_INVALHANDLE;
3523
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;
3565     }
3566
3567     return MMSYSERR_NOTSUPPORTED;
3568 }
3569
3570 static UINT WINMM_GetSourceLineInfo(WINMM_MMDevice *mmdevice, UINT mmdev_index,
3571         MIXERLINEW *info, DWORD flags)
3572 {
3573     BOOL is_out = TRUE;
3574     if(mmdevice->in_caps.szPname[0] != '\0')
3575         is_out = FALSE;
3576
3577     if(info->dwSource != 0)
3578         return MIXERR_INVALLINE;
3579
3580     info->dwDestination = 0;
3581     info->dwLineID = 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;
3591
3592     lstrcpyW(info->szShortName, volumeW);
3593     lstrcpyW(info->szName, mastervolumeW);
3594
3595     if(is_out){
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));
3600     }else{
3601         info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_LINE;
3602         info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
3603         info->Target.szPname[0] = '\0';
3604     }
3605
3606     return MMSYSERR_NOERROR;
3607 }
3608
3609 static UINT WINMM_GetDestinationLineInfo(WINMM_MMDevice *mmdevice,
3610         UINT mmdev_index, MIXERLINEW *info, DWORD flags)
3611 {
3612     BOOL is_out = TRUE;
3613     if(mmdevice->in_caps.szPname[0] != '\0')
3614         is_out = FALSE;
3615
3616     if(info->dwDestination != 0)
3617         return MIXERR_INVALLINE;
3618
3619     info->dwSource = 0xFFFFFFFF;
3620     info->dwLineID = 0xFFFF0000;
3621     info->fdwLine = MIXERLINE_LINEF_ACTIVE;
3622     info->cConnections = 1;
3623     info->cControls = 2;
3624
3625     lstrcpyW(info->szShortName, volumeW);
3626     lstrcpyW(info->szName, mastervolumeW);
3627
3628     info->Target.dwDeviceID = mmdev_index;
3629     info->Target.wMid = ~0;
3630     info->Target.wPid = ~0;
3631     info->Target.vDriverVersion = 0;
3632
3633     if(is_out){
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';
3638     }else{
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));
3644     }
3645
3646     return MMSYSERR_NOERROR;
3647 }
3648
3649 static UINT WINMM_GetComponentTypeLineInfo(WINMM_MMDevice *mmdevice,
3650         UINT mmdev_index, MIXERLINEW *info, DWORD flags)
3651 {
3652     BOOL is_out = TRUE;
3653     if(mmdevice->in_caps.szPname[0] != '\0')
3654         is_out = FALSE;
3655
3656     if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_WAVEIN){
3657         if(is_out)
3658             return MIXERR_INVALLINE;
3659         info->dwDestination = 0;
3660         return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
3661     }
3662
3663     if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_SPEAKERS){
3664         if(!is_out)
3665             return MIXERR_INVALLINE;
3666         info->dwDestination = 0;
3667         return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
3668     }
3669
3670     if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_LINE){
3671         if(is_out)
3672             return MIXERR_INVALLINE;
3673         info->dwSource = 0;
3674         return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
3675     }
3676
3677     if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT){
3678         if(!is_out)
3679             return MIXERR_INVALLINE;
3680         info->dwSource = 0;
3681         return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
3682     }
3683
3684     TRACE("Returning INVALLINE on this component type: %u\n",
3685             info->dwComponentType);
3686
3687     return MIXERR_INVALLINE;
3688 }
3689
3690 static UINT WINMM_GetLineIDLineInfo(WINMM_MMDevice *mmdevice,
3691         UINT mmdev_index, MIXERLINEW *info, DWORD flags)
3692 {
3693     if(info->dwLineID == 0xFFFF0000){
3694         info->dwDestination = 0;
3695         return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
3696     }
3697
3698     if(info->dwLineID == 0){
3699         info->dwSource = 0;
3700         return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
3701     }
3702
3703     TRACE("Returning INVALLINE on this dwLineID: %u\n", info->dwLineID);
3704     return MIXERR_INVALLINE;
3705 }
3706
3707 /**************************************************************************
3708  *                              mixerGetLineInfoW               [WINMM.@]
3709  */
3710 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
3711 {
3712     UINT mmdev_index;
3713     WINMM_MMDevice *mmdevice;
3714
3715     TRACE("(%p, %p, %x)\n", hmix, lpmliW, fdwInfo);
3716
3717     if(!WINMM_StartDevicesThread())
3718         return MMSYSERR_ERROR;
3719
3720     if(!lpmliW || lpmliW->cbStruct < sizeof(MIXERLINEW))
3721         return MMSYSERR_INVALPARAM;
3722
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);
3728
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;
3738     }
3739
3740     mmdevice = WINMM_GetMixerMMDevice(hmix, fdwInfo, &mmdev_index);
3741     if(!mmdevice)
3742         return MMSYSERR_INVALHANDLE;
3743
3744     switch(fdwInfo & MIXER_GETLINEINFOF_QUERYMASK){
3745     case MIXER_GETLINEINFOF_DESTINATION:
3746         return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, lpmliW,
3747                 fdwInfo);
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,
3752                 fdwInfo);
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;
3758     }
3759
3760     TRACE("Returning INVALFLAG on these flags: %lx\n",
3761             fdwInfo & MIXER_GETLINEINFOF_QUERYMASK);
3762     return MMSYSERR_INVALFLAG;
3763 }
3764
3765 /**************************************************************************
3766  *                              mixerGetLineInfoA               [WINMM.@]
3767  */
3768 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
3769                               DWORD fdwInfo)
3770 {
3771     MIXERLINEW          mliW;
3772     UINT                ret;
3773
3774     TRACE("(%p, %p, %x)\n", hmix, lpmliA, fdwInfo);
3775
3776     if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
3777         return MMSYSERR_INVALPARAM;
3778
3779     mliW.cbStruct = sizeof(mliW);
3780     switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
3781     case MIXER_GETLINEINFOF_COMPONENTTYPE:
3782         mliW.dwComponentType = lpmliA->dwComponentType;
3783         break;
3784     case MIXER_GETLINEINFOF_DESTINATION:
3785         mliW.dwDestination = lpmliA->dwDestination;
3786         break;
3787     case MIXER_GETLINEINFOF_LINEID:
3788         mliW.dwLineID = lpmliA->dwLineID;
3789         break;
3790     case MIXER_GETLINEINFOF_SOURCE:
3791         mliW.dwDestination = lpmliA->dwDestination;
3792         mliW.dwSource = lpmliA->dwSource;
3793         break;
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));
3800         break;
3801     default:
3802         WARN("Unsupported fdwControls=0x%08x\n", fdwInfo);
3803         return MMSYSERR_INVALFLAG;
3804     }
3805
3806     ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
3807
3808     if(ret == MMSYSERR_NOERROR)
3809     {
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 );
3830     }
3831     return ret;
3832 }
3833
3834 /**************************************************************************
3835  *                              mixerSetControlDetails  [WINMM.@]
3836  */
3837 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
3838                                    DWORD fdwDetails)
3839 {
3840     WINMM_ControlDetails details;
3841
3842     TRACE("(%p, %p, %x)\n", hmix, lpmcd, fdwDetails);
3843
3844     if(!WINMM_StartDevicesThread())
3845         return MMSYSERR_ERROR;
3846
3847     if((fdwDetails & MIXER_SETCONTROLDETAILSF_QUERYMASK) ==
3848             MIXER_SETCONTROLDETAILSF_CUSTOM)
3849         return MMSYSERR_NOTSUPPORTED;
3850
3851     if(!lpmcd)
3852         return MMSYSERR_INVALPARAM;
3853
3854     TRACE("dwControlID: %u\n", lpmcd->dwControlID);
3855
3856     details.hmix = hmix;
3857     details.details = lpmcd;
3858     details.flags = fdwDetails;
3859
3860     return SendMessageW(g_devices_hwnd, MXDM_SETCONTROLDETAILS,
3861             (DWORD_PTR)&details, 0);
3862 }
3863
3864 /**************************************************************************
3865  *                              mixerMessage            [WINMM.@]
3866  */
3867 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3868 {
3869     TRACE("(%p, %d, %lx, %lx)\n", hmix, uMsg, dwParam1, dwParam2);
3870
3871     return MMSYSERR_NOTSUPPORTED;
3872 }