2 * Alsa MIXER Wine Driver for Linux
3 * Very loosely based on wineoss mixer driver
5 * Copyright 2007 Maarten Lankhorst
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
35 #ifdef HAVE_SYS_IOCTL_H
36 # include <sys/ioctl.h>
39 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
51 #include "wine/unicode.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(mixer);
58 #define WINE_MIXER_MANUF_ID 0xAA
59 #define WINE_MIXER_PRODUCT_ID 0x55
60 #define WINE_MIXER_VERSION 0x0100
63 * In windows it seems to be required for all controls to have a volume switch
64 * In alsa that's optional
66 * I assume for playback controls, that there is always a playback volume switch available
69 * For capture controls, it is needed that there is a capture switch and a volume switch,
70 * It doesn't matter whether it is a playback volume switch or a capture volume switch.
71 * The code will first try to get/adjust capture volume, if that fails it tries playback volume
72 * It is not pretty, but under my 3 test cards it seems that there is no other choice:
73 * Most capture controls don't have a capture volume setting
75 * MUX means that only capture source can be exclusively selected,
76 * MIXER means that multiple sources can be selected simultaneously.
79 static const char * getMessage(UINT uMsg)
82 #define MSG_TO_STR(x) case x: return #x;
84 MSG_TO_STR(DRVM_INIT);
85 MSG_TO_STR(DRVM_EXIT);
86 MSG_TO_STR(DRVM_ENABLE);
87 MSG_TO_STR(DRVM_DISABLE);
88 MSG_TO_STR(MXDM_GETDEVCAPS);
89 MSG_TO_STR(MXDM_GETLINEINFO);
90 MSG_TO_STR(MXDM_GETNUMDEVS);
91 MSG_TO_STR(MXDM_OPEN);
92 MSG_TO_STR(MXDM_CLOSE);
93 MSG_TO_STR(MXDM_GETLINECONTROLS);
94 MSG_TO_STR(MXDM_GETCONTROLDETAILS);
95 MSG_TO_STR(MXDM_SETCONTROLDETAILS);
99 sprintf(str, "UNKNOWN(%08x)", uMsg);
103 static const char * getControlType(DWORD dwControlType)
105 #define TYPE_TO_STR(x) case x: return #x;
106 switch (dwControlType) {
107 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_CUSTOM);
108 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BOOLEANMETER);
109 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SIGNEDMETER);
110 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PEAKMETER);
111 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_UNSIGNEDMETER);
112 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BOOLEAN);
113 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_ONOFF);
114 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MUTE);
115 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MONO);
116 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_LOUDNESS);
117 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_STEREOENH);
118 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BASS_BOOST);
119 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BUTTON);
120 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_DECIBELS);
121 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SIGNED);
122 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_UNSIGNED);
123 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PERCENT);
124 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SLIDER);
125 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PAN);
126 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_QSOUNDPAN);
127 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_FADER);
128 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_VOLUME);
129 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BASS);
130 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_TREBLE);
131 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_EQUALIZER);
132 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SINGLESELECT);
133 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MUX);
134 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MULTIPLESELECT);
135 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MIXER);
136 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MICROTIME);
137 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MILLITIME);
140 return wine_dbg_sprintf("UNKNOWN(%08x)", dwControlType);
143 /* A simple declaration of a line control
144 * These are each of the channels that show up
146 typedef struct line {
147 /* Name we present to outside world */
148 WCHAR name[MAXPNAMELEN];
154 snd_mixer_elem_t *elem;
157 /* A control structure, with toggle enabled switch
158 * Control structures control volume, muted, which capture source
160 typedef struct control {
169 WCHAR mixername[MAXPNAMELEN];
172 LPDRVCALLBACK callback;
173 DWORD_PTR callbackpriv;
180 #define MAX_MIXERS 32
181 #define CONTROLSPERLINE 3
185 static int cards = 0;
186 static mixer mixdev[MAX_MIXERS];
187 static HANDLE thread;
188 static int elem_callback(snd_mixer_elem_t *elem, unsigned int mask);
189 static DWORD WINAPI ALSA_MixerPollThread(LPVOID lParam);
190 static CRITICAL_SECTION elem_crst;
191 static int msg_pipe[2];
194 /* found channel names in alsa lib, alsa api doesn't have another way for this
195 * map name -> componenttype, worst case we get a wrong componenttype which is
199 static const struct mixerlinetype {
200 const char *name; DWORD cmpt;
202 { "Master", MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, },
203 { "Capture", MIXERLINE_COMPONENTTYPE_DST_WAVEIN, },
204 { "PCM", MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT, },
205 { "PC Speaker", MIXERLINE_COMPONENTTYPE_SRC_PCSPEAKER, },
206 { "Synth", MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER, },
207 { "Headphone", MIXERLINE_COMPONENTTYPE_DST_HEADPHONES, },
208 { "Mic", MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, },
209 { "Aux", MIXERLINE_COMPONENTTYPE_SRC_UNDEFINED, },
210 { "CD", MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC, },
211 { "Line", MIXERLINE_COMPONENTTYPE_SRC_LINE, },
212 { "Phone", MIXERLINE_COMPONENTTYPE_SRC_TELEPHONE, },
215 /* Map name to MIXERLINE_COMPONENTTYPE_XXX */
216 static int getcomponenttype(const char *name)
219 for (x=0; x< sizeof(converttable)/sizeof(converttable[0]); ++x)
220 if (!strcasecmp(name, converttable[x].name))
222 TRACE("%d -> %s\n", x, name);
223 return converttable[x].cmpt;
225 WARN("Unknown mixer name %s, probably harmless\n", name);
226 return MIXERLINE_COMPONENTTYPE_SRC_UNDEFINED;
229 /* Is this control suited for showing up? */
230 static int blacklisted(snd_mixer_elem_t *elem)
232 const char *name = snd_mixer_selem_get_name(elem);
235 if (!snd_mixer_selem_has_playback_volume(elem) &&
236 (!snd_mixer_selem_has_capture_volume(elem) ||
237 !snd_mixer_selem_has_capture_switch(elem)))
240 TRACE("%s: %x\n", name, blisted);
244 static void fillcontrols(mixer *mmixer)
247 for (id = 0; id < mmixer->chans; ++id)
249 line *mline = &mmixer->lines[id];
250 int ofs = CONTROLSPERLINE * id;
254 TRACE("Filling control %d\n", id);
255 if (id == 1 && !mline->elem)
258 if (mline->capt && snd_mixer_selem_has_capture_volume(mline->elem))
259 snd_mixer_selem_get_capture_volume_range(mline->elem, &min, &max);
261 snd_mixer_selem_get_playback_volume_range(mline->elem, &min, &max);
263 /* (!snd_mixer_selem_has_playback_volume(elem) || snd_mixer_selem_has_capture_volume(elem)) */
264 /* Volume, always enabled by definition of blacklisted channels */
265 mmixer->controls[ofs].enabled = 1;
266 mmixer->controls[ofs].c.cbStruct = sizeof(mmixer->controls[ofs].c);
267 mmixer->controls[ofs].c.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
268 mmixer->controls[ofs].c.dwControlID = ofs;
269 mmixer->controls[ofs].c.Bounds.s1.dwMinimum = 0;
270 mmixer->controls[ofs].c.Bounds.s1.dwMaximum = 65535;
271 mmixer->controls[ofs].c.Metrics.cSteps = 65536/(max-min);
273 if ((id == 1 && snd_mixer_selem_has_capture_switch(mline->elem)) ||
274 (!mline->capt && snd_mixer_selem_has_playback_switch(mline->elem)))
275 { /* MUTE button optional, main capture channel should have one too */
276 mmixer->controls[ofs+OFS_MUTE].enabled = 1;
277 mmixer->controls[ofs+OFS_MUTE].c.cbStruct = sizeof(mmixer->controls[ofs].c);
278 mmixer->controls[ofs+OFS_MUTE].c.dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
279 mmixer->controls[ofs+OFS_MUTE].c.dwControlID = ofs+OFS_MUTE;
280 mmixer->controls[ofs+OFS_MUTE].c.Bounds.s1.dwMaximum = 1;
283 if (mline->capt && snd_mixer_selem_has_capture_switch_exclusive(mline->elem))
284 mmixer->controls[CONTROLSPERLINE+OFS_MUX].c.dwControlType = MIXERCONTROL_CONTROLTYPE_MUX;
287 { /* Capture select, in case cMultipleItems is 0, it means capture is disabled anyway */
288 mmixer->controls[ofs+OFS_MUX].enabled = 1;
289 mmixer->controls[ofs+OFS_MUX].c.cbStruct = sizeof(mmixer->controls[ofs].c);
290 mmixer->controls[ofs+OFS_MUX].c.dwControlType = MIXERCONTROL_CONTROLTYPE_MIXER;
291 mmixer->controls[ofs+OFS_MUX].c.dwControlID = ofs+OFS_MUX;
292 mmixer->controls[ofs+OFS_MUX].c.fdwControl = MIXERCONTROL_CONTROLF_MULTIPLE;
294 for (x = 0; x<mmixer->chans; ++x)
295 if (x != id && mmixer->lines[x].dst == id)
296 ++(mmixer->controls[ofs+OFS_MUX].c.cMultipleItems);
297 if (!mmixer->controls[ofs+OFS_MUX].c.cMultipleItems)
298 mmixer->controls[ofs+OFS_MUX].enabled = 0;
300 mmixer->controls[ofs+OFS_MUX].c.Bounds.s1.dwMaximum = mmixer->controls[ofs+OFS_MUX].c.cMultipleItems - 1;
301 mmixer->controls[ofs+OFS_MUX].c.Metrics.cSteps = mmixer->controls[ofs+OFS_MUX].c.cMultipleItems;
303 for (x=0; x<CONTROLSPERLINE; ++x)
305 lstrcpynW(mmixer->controls[ofs+x].c.szShortName, mline->name, sizeof(mmixer->controls[ofs+x].c.szShortName)/sizeof(WCHAR));
306 lstrcpynW(mmixer->controls[ofs+x].c.szName, mline->name, sizeof(mmixer->controls[ofs+x].c.szName)/sizeof(WCHAR));
311 /* get amount of channels for elem */
312 /* Officially we should keep capture/playback separated,
313 * but that's not going to work in the alsa api */
314 static int chans(mixer *mmixer, snd_mixer_elem_t * elem, DWORD capt)
318 if (capt && snd_mixer_selem_has_capture_volume(elem)) {
319 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
320 if (snd_mixer_selem_has_capture_channel(elem, chn))
323 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
324 if (snd_mixer_selem_has_playback_channel(elem, chn))
328 FIXME("Mixer channel %s was found for %s, but no channels were found? Wrong selection!\n", snd_mixer_selem_get_name(elem), (snd_mixer_selem_has_playback_volume(elem) ? "playback" : "capture"));
332 static void filllines(mixer *mmixer, snd_mixer_elem_t *mastelem, snd_mixer_elem_t *captelem, int capt)
334 snd_mixer_elem_t *elem;
335 line *mline = mmixer->lines;
338 MultiByteToWideChar(CP_UNIXCP, 0, snd_mixer_selem_get_name(mastelem), -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
339 mline->component = getcomponenttype(snd_mixer_selem_get_name(mastelem));
342 mline->elem = mastelem;
343 mline->chans = chans(mmixer, mastelem, 0);
345 snd_mixer_elem_set_callback(mastelem, &elem_callback);
346 snd_mixer_elem_set_callback_private(mastelem, &mmixer);
349 * Note: since mmixer->dests = 1, it means only playback control is visible
350 * This makes sense, because if there are no capture sources capture control
351 * can't do anything and should be invisible */
353 /* Control 1 is reserved for capture even when not enabled */
357 MultiByteToWideChar(CP_UNIXCP, 0, snd_mixer_selem_get_name(captelem), -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
358 mline->component = getcomponenttype(snd_mixer_selem_get_name(captelem));
361 mline->elem = captelem;
362 mline->chans = chans(mmixer, captelem, 1);
364 snd_mixer_elem_set_callback(captelem, &elem_callback);
365 snd_mixer_elem_set_callback_private(captelem, &mmixer);
368 for (elem = snd_mixer_first_elem(mmixer->mix); elem; elem = snd_mixer_elem_next(elem))
369 if (elem != mastelem && elem != captelem && !blacklisted(elem))
371 const char * name = snd_mixer_selem_get_name(elem);
372 DWORD comp = getcomponenttype(name);
374 if (snd_mixer_selem_has_playback_volume(elem))
376 (++mline)->component = comp;
377 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, mline->name, MAXPNAMELEN);
378 mline->capt = mline->dst = 0;
380 mline->chans = chans(mmixer, elem, 0);
385 if (capt && snd_mixer_selem_has_capture_switch(elem))
387 (++mline)->component = comp;
388 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, mline->name, MAXPNAMELEN);
389 mline->capt = mline->dst = 1;
391 mline->chans = chans(mmixer, elem, 1);
394 snd_mixer_elem_set_callback(elem, &elem_callback);
395 snd_mixer_elem_set_callback_private(elem, &mmixer);
399 /* Windows api wants to have a 'master' device to which all slaves are attached
400 * There are 2 ones in this code:
401 * - 'Master', fall back to 'Headphone' if unavailable, and if that's not available 'PCM'
403 * Capture might not always be available, so should be prepared to be without if needed
406 static void ALSA_MixerInit(void)
410 for (x = 0; x < MAX_MIXERS; ++x)
412 int card, err, capcontrols = 0;
413 char cardind[6], cardname[10];
416 snd_mixer_elem_t *elem, *mastelem = NULL, *headelem = NULL, *captelem = NULL, *pcmelem = NULL;
417 snd_ctl_card_info_t *info = NULL;
418 snd_ctl_card_info_alloca(&info);
420 memset(&mixdev[mixnum], 0, sizeof(*mixdev));
421 snprintf(cardind, sizeof(cardind), "%d", x);
422 card = snd_card_get_index(cardind);
426 snprintf(cardname, sizeof(cardname), "hw:%d", card);
428 err = snd_ctl_open(&ctl, cardname, 0);
431 WARN("Cannot open card: %s\n", snd_strerror(err));
435 err = snd_ctl_card_info(ctl, info);
438 WARN("Cannot get card info: %s\n", snd_strerror(err));
443 MultiByteToWideChar(CP_UNIXCP, 0, snd_ctl_card_info_get_name(info), -1, mixdev[mixnum].mixername, sizeof(mixdev[mixnum].mixername)/sizeof(WCHAR));
446 err = snd_mixer_open(&mixdev[mixnum].mix, 0);
449 WARN("Error occurred opening mixer: %s\n", snd_strerror(err));
453 err = snd_mixer_attach(mixdev[mixnum].mix, cardname);
457 err = snd_mixer_selem_register(mixdev[mixnum].mix, NULL, NULL);
461 err = snd_mixer_load(mixdev[mixnum].mix);
465 /* First, lets see what's available..
466 * If there are multiple Master or Captures, all except 1 will be added as slaves
468 for (elem = snd_mixer_first_elem(mixdev[mixnum].mix); elem; elem = snd_mixer_elem_next(elem))
469 if (!strcasecmp(snd_mixer_selem_get_name(elem), "Master") && !mastelem)
471 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "Capture") && !captelem)
473 else if (!blacklisted(elem))
475 if (snd_mixer_selem_has_capture_switch(elem))
477 if (snd_mixer_selem_has_playback_volume(elem))
479 if (!strcasecmp(snd_mixer_selem_get_name(elem), "Headphone") && !headelem)
481 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "PCM") && !pcmelem)
484 ++(mixdev[mixnum].chans);
488 /* Add master channel, uncounted channels and an extra for capture */
489 mixdev[mixnum].chans += !!mastelem + !!headelem + !!pcmelem + 1;
491 /* If there is only 'Capture' and 'Master', this device is not worth it */
492 if (mixdev[mixnum].chans == 2)
494 WARN("No channels found, skipping device!\n");
498 /* Master element can't have a capture control in this code, so
499 * if Headphone or PCM is promoted to master, unset its capture control */
500 if (headelem && !mastelem)
502 /* Using 'Headphone' as master device */
504 capcontrols -= !!snd_mixer_selem_has_capture_switch(mastelem);
506 else if (pcmelem && !mastelem)
508 /* Use 'PCM' as master device */
510 capcontrols -= !!snd_mixer_selem_has_capture_switch(mastelem);
514 /* If there is nothing sensible that can act as 'Master' control, something is wrong */
515 FIXME("No master control found, disabling mixer\n");
519 if (!captelem || !capcontrols)
521 /* Can't enable capture, so disabling it
522 * Note: capture control will still exist because
523 * dwLineID 0 and 1 are reserved for Master and Capture
525 WARN("No use enabling capture part of mixer, capture control found: %s, amount of capture controls: %d\n",
526 (!captelem ? "no" : "yes"), capcontrols);
528 mixdev[mixnum].dests = 1;
532 mixdev[mixnum].chans += capcontrols;
533 mixdev[mixnum].dests = 2;
536 mixdev[mixnum].lines = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(line) * mixdev[mixnum].chans);
537 mixdev[mixnum].controls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(control) * CONTROLSPERLINE*mixdev[mixnum].chans);
539 if (!mixdev[mixnum].lines || !mixdev[mixnum].controls)
542 filllines(&mixdev[mixnum], mastelem, captelem, capcontrols);
543 fillcontrols(&mixdev[mixnum]);
545 TRACE("%s: Amount of controls: %i/%i, name: %s\n", cardname, mixdev[mixnum].dests, mixdev[mixnum].chans, debugstr_w(mixdev[mixnum].mixername));
550 WARN("Error occurred initialising mixer: %s\n", snd_strerror(err));
552 HeapFree(GetProcessHeap(), 0, mixdev[mixnum].lines);
553 HeapFree(GetProcessHeap(), 0, mixdev[mixnum].controls);
554 snd_mixer_close(mixdev[mixnum].mix);
558 /* There is no trouble with already assigning callbacks without initialising critsect:
559 * Callbacks only occur when snd_mixer_handle_events is called (only happens in thread)
561 InitializeCriticalSection(&elem_crst);
562 elem_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_MIXER.elem_crst");
566 static void ALSA_MixerExit(void)
572 WARN("Callback thread still alive, terminating uncleanly, refcnt: %d\n", refcnt);
573 /* Least we can do is making sure we're not in 'foreign' code */
574 EnterCriticalSection(&elem_crst);
575 TerminateThread(thread, 1);
579 TRACE("Cleaning up\n");
581 elem_crst.DebugInfo->Spare[0] = 0;
582 DeleteCriticalSection(&elem_crst);
583 for (x = 0; x < cards; ++x)
585 snd_mixer_close(mixdev[x].mix);
586 HeapFree(GetProcessHeap(), 0, mixdev[x].lines);
587 HeapFree(GetProcessHeap(), 0, mixdev[x].controls);
592 static mixer* MIX_GetMix(UINT wDevID)
596 if (wDevID < 0 || wDevID >= cards)
598 WARN("Invalid mixer id: %d\n", wDevID);
602 mmixer = &mixdev[wDevID];
606 /* Since alsa doesn't tell what exactly changed, just assume all affected controls changed */
607 static int elem_callback(snd_mixer_elem_t *elem, unsigned int type)
609 mixer *mmixer = snd_mixer_elem_get_callback_private(elem);
611 BOOL captchanged = 0;
613 if (type != SND_CTL_EVENT_MASK_VALUE)
618 EnterCriticalSection(&elem_crst);
620 if (!mmixer->callback)
623 for (x=0; x<mmixer->chans; ++x)
625 const int ofs = CONTROLSPERLINE*x;
626 if (elem != mmixer->lines[x].elem)
629 if (mmixer->lines[x].capt)
632 TRACE("Found changed control %s\n", debugstr_w(mmixer->lines[x].name));
633 mmixer->callback(mmixer->hmx, MM_MIXM_LINE_CHANGE, mmixer->callbackpriv, x, 0);
634 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, ofs, 0);
636 if (mmixer->controls[ofs+OFS_MUTE].enabled)
637 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, ofs+OFS_MUTE, 0);
640 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, CONTROLSPERLINE+OFS_MUX, 0);
643 LeaveCriticalSection(&elem_crst);
648 static DWORD WINAPI ALSA_MixerPollThread(LPVOID lParam)
650 struct pollfd *pfds = NULL;
651 int x, y, err, mcnt, count = 1;
653 TRACE("%p\n", lParam);
655 for (x = 0; x < cards; ++x)
656 count += snd_mixer_poll_descriptors_count(mixdev[x].mix);
658 TRACE("Counted %d descriptors\n", count);
659 pfds = HeapAlloc(GetProcessHeap(), 0, count * sizeof(struct pollfd));
663 WARN("Out of memory\n");
667 pfds[0].fd = msg_pipe[0];
668 pfds[0].events = POLLIN;
671 for (x = 0; x < cards; ++x)
672 y += snd_mixer_poll_descriptors(mixdev[x].mix, &pfds[y], count - y);
674 while ((err = poll(pfds, (unsigned int) count, -1)) >= 0 || errno == EINTR || errno == EAGAIN)
676 if (pfds[0].revents & POLLIN)
680 for (x = y = 0; x < cards; ++x)
682 int j, max = snd_mixer_poll_descriptors_count(mixdev[x].mix);
683 for (j = 0; j < max; ++j)
684 if (pfds[mcnt+j].revents)
686 y += snd_mixer_handle_events(mixdev[x].mix);
692 TRACE("Handled %d events\n", y);
696 TRACE("Shutting down\n");
697 HeapFree(GetProcessHeap(), 0, pfds);
699 y = read(msg_pipe[0], &x, sizeof(x));
705 static DWORD MIX_Open(UINT wDevID, LPMIXEROPENDESC desc, DWORD_PTR flags)
707 mixer *mmixer = MIX_GetMix(wDevID);
709 return MMSYSERR_BADDEVICEID;
711 flags &= CALLBACK_TYPEMASK;
717 case CALLBACK_FUNCTION:
721 FIXME("Unhandled callback type: %08lx\n", flags & CALLBACK_TYPEMASK);
722 return MIXERR_INVALVALUE;
725 mmixer->callback = (LPDRVCALLBACK)desc->dwCallback;
726 mmixer->callbackpriv = desc->dwInstance;
727 mmixer->hmx = (HDRVR)desc->hmx;
730 if (InterlockedIncrement(&refcnt) == 1)
732 if (pipe(msg_pipe) >= 0)
734 thread = CreateThread(NULL, 0, ALSA_MixerPollThread, NULL, 0, NULL);
739 msg_pipe[0] = msg_pipe[1] = -1;
743 msg_pipe[0] = msg_pipe[1] = -1;
746 return MMSYSERR_NOERROR;
749 static DWORD MIX_Close(UINT wDevID)
752 mixer *mmixer = MIX_GetMix(wDevID);
754 return MMSYSERR_BADDEVICEID;
756 EnterCriticalSection(&elem_crst);
757 mmixer->callback = 0;
758 LeaveCriticalSection(&elem_crst);
760 if (!InterlockedDecrement(&refcnt))
762 if (write(msg_pipe[1], &x, sizeof(x)) > 0)
764 TRACE("Shutting down thread...\n");
765 WaitForSingleObject(thread, INFINITE);
770 return MMSYSERR_NOERROR;
773 static DWORD MIX_GetDevCaps(UINT wDevID, LPMIXERCAPS2W caps, DWORD_PTR parm2)
775 mixer *mmixer = MIX_GetMix(wDevID);
779 return MMSYSERR_INVALPARAM;
782 return MMSYSERR_BADDEVICEID;
784 memset(&capsW, 0, sizeof(MIXERCAPS2W));
786 capsW.wMid = WINE_MIXER_MANUF_ID;
787 capsW.wPid = WINE_MIXER_PRODUCT_ID;
788 capsW.vDriverVersion = WINE_MIXER_VERSION;
790 lstrcpynW(capsW.szPname, mmixer->mixername, sizeof(capsW.szPname)/sizeof(WCHAR));
791 capsW.cDestinations = mmixer->dests;
792 memcpy(caps, &capsW, min(parm2, sizeof(capsW)));
793 return MMSYSERR_NOERROR;
796 /* convert win32 volume to alsa volume, and vice versa */
797 static INT normalized(INT value, INT prevmax, INT nextmax)
799 int ret = MulDiv(value, nextmax, prevmax);
801 /* Have to stay in range */
802 TRACE("%d/%d -> %d/%d\n", value, prevmax, ret, nextmax);
811 /* get amount of sources for dest */
812 static int getsrccntfromchan(mixer *mmixer, int dad)
816 for (i=0; i<mmixer->chans; ++i)
817 if (i != dad && mmixer->lines[i].dst == dad)
822 FIXME("No src found for %i (%s)?\n", dad, debugstr_w(mmixer->lines[dad].name));
826 /* find lineid for source 'num' with dest 'dad' */
827 static int getsrclinefromchan(mixer *mmixer, int dad, int num)
830 for (i=0; i<mmixer->chans; ++i)
831 if (i != dad && mmixer->lines[i].dst == dad)
837 WARN("No src found for src %i from dest %i\n", num, dad);
841 /* get the source number belonging to line */
842 static int getsrcfromline(mixer *mmixer, int line)
844 int i, j=0, dad = mmixer->lines[line].dst;
846 for (i=0; i<mmixer->chans; ++i)
847 if (i != dad && mmixer->lines[i].dst == dad)
853 WARN("No src found for line %i with dad %i\n", line, dad);
857 /* Get volume/muted/capture channel */
858 static DWORD MIX_GetControlDetails(UINT wDevID, LPMIXERCONTROLDETAILS mctrld, DWORD_PTR flags)
860 mixer *mmixer = MIX_GetMix(wDevID);
866 return MMSYSERR_INVALPARAM;
868 ctrl = mctrld->dwControlID;
869 line = ctrl/CONTROLSPERLINE;
871 if (mctrld->cbStruct != sizeof(*mctrld))
872 return MMSYSERR_INVALPARAM;
875 return MMSYSERR_BADDEVICEID;
877 if (line < 0 || line >= mmixer->chans || !mmixer->controls[ctrl].enabled)
878 return MIXERR_INVALCONTROL;
880 ct = &mmixer->controls[ctrl];
882 flags &= MIXER_GETCONTROLDETAILSF_QUERYMASK;
885 case MIXER_GETCONTROLDETAILSF_VALUE:
886 TRACE("MIXER_GETCONTROLDETAILSF_VALUE (%d/%d)\n", ctrl, line);
887 switch (ct->c.dwControlType)
889 case MIXERCONTROL_CONTROLTYPE_VOLUME:
891 long min = 0, max = 0, vol = 0;
893 LPMIXERCONTROLDETAILS_UNSIGNED mcdu;
894 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
896 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_UNSIGNED))
898 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
899 return MMSYSERR_INVALPARAM;
902 TRACE("%s MIXERCONTROLDETAILS_UNSIGNED[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
904 mcdu = (LPMIXERCONTROLDETAILS_UNSIGNED)mctrld->paDetails;
906 if (mctrld->cChannels != 1 && mmixer->lines[line].chans != mctrld->cChannels)
908 WARN("Unsupported cChannels (%d instead of %d)\n", mctrld->cChannels, mmixer->lines[line].chans);
909 return MMSYSERR_INVALPARAM;
912 if (mmixer->lines[line].capt && snd_mixer_selem_has_capture_volume(elem)) {
913 snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
914 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
915 if (snd_mixer_selem_has_capture_channel(elem, chn))
917 snd_mixer_selem_get_capture_volume(elem, chn, &vol);
918 mcdu->dwValue = normalized(vol - min, max, 65535);
919 if (mctrld->cChannels == 1)
924 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
926 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
927 if (snd_mixer_selem_has_playback_channel(elem, chn))
929 snd_mixer_selem_get_playback_volume(elem, chn, &vol);
930 mcdu->dwValue = normalized(vol - min, max, 65535);
931 if (mctrld->cChannels == 1)
937 return MMSYSERR_NOERROR;
940 case MIXERCONTROL_CONTROLTYPE_ONOFF:
941 case MIXERCONTROL_CONTROLTYPE_MUTE:
943 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
945 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
947 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
949 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
950 return MMSYSERR_INVALPARAM;
953 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
955 mcdb = (LPMIXERCONTROLDETAILS_BOOLEAN)mctrld->paDetails;
958 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
960 if (!snd_mixer_selem_has_capture_channel(elem, chn))
962 snd_mixer_selem_get_capture_switch(elem, chn, &ival);
966 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
968 if (!snd_mixer_selem_has_playback_channel(elem, chn))
970 snd_mixer_selem_get_playback_switch(elem, chn, &ival);
974 mcdb->fValue = !ival;
975 TRACE("=> %s\n", mcdb->fValue ? "on" : "off");
976 return MMSYSERR_NOERROR;
978 case MIXERCONTROL_CONTROLTYPE_MIXER:
979 case MIXERCONTROL_CONTROLTYPE_MUX:
981 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
982 int x, i=0, ival = 0, chn;
984 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
986 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
987 return MMSYSERR_INVALPARAM;
990 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
992 mcdb = (LPMIXERCONTROLDETAILS_BOOLEAN)mctrld->paDetails;
994 for (x = 0; x<mmixer->chans; ++x)
995 if (line != x && mmixer->lines[x].dst == line)
998 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1000 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1002 snd_mixer_selem_get_capture_switch(mmixer->lines[x].elem, chn, &ival);
1006 if (i >= mctrld->u.cMultipleItems)
1008 TRACE("overflow\n");
1009 return MMSYSERR_INVALPARAM;
1011 TRACE("fVal[%i] = %sselected\n", i, (!ival ? "un" : ""));
1012 mcdb[i++].fValue = ival;
1018 FIXME("Unhandled controltype %s\n", getControlType(ct->c.dwControlType));
1019 return MMSYSERR_INVALPARAM;
1021 return MMSYSERR_NOERROR;
1023 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
1024 TRACE("MIXER_GETCONTROLDETAILSF_LISTTEXT (%d)\n", ctrl);
1026 if (ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MUX || ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MIXER)
1028 LPMIXERCONTROLDETAILS_LISTTEXTW mcdlt = (LPMIXERCONTROLDETAILS_LISTTEXTW)mctrld->paDetails;
1031 for (i = j = 0; j < mmixer->chans; ++j)
1032 if (j != line && mmixer->lines[j].dst == line)
1034 if (i > mctrld->u.cMultipleItems)
1035 return MMSYSERR_INVALPARAM;
1036 mcdlt->dwParam1 = j;
1037 mcdlt->dwParam2 = mmixer->lines[j].component;
1038 lstrcpynW(mcdlt->szName, mmixer->lines[j].name, sizeof(mcdlt->szName) / sizeof(WCHAR));
1039 TRACE("Adding %i as %s\n", j, debugstr_w(mcdlt->szName));
1042 if (i < mctrld->u.cMultipleItems)
1043 return MMSYSERR_INVALPARAM;
1044 return MMSYSERR_NOERROR;
1046 FIXME ("Imagine this code being horribly broken and incomplete, introducing: reality\n");
1047 return MMSYSERR_INVALPARAM;
1050 WARN("Unknown flag (%08lx)\n", flags);
1051 return MMSYSERR_INVALPARAM;
1055 /* Set volume/capture channel/muted for control */
1056 static DWORD MIX_SetControlDetails(UINT wDevID, LPMIXERCONTROLDETAILS mctrld, DWORD_PTR flags)
1058 mixer *mmixer = MIX_GetMix(wDevID);
1059 DWORD ctrl, line, i;
1061 snd_mixer_elem_t * elem;
1064 return MMSYSERR_INVALPARAM;
1066 ctrl = mctrld->dwControlID;
1067 line = ctrl/CONTROLSPERLINE;
1069 if (mctrld->cbStruct != sizeof(*mctrld))
1071 WARN("Invalid size of mctrld %d\n", mctrld->cbStruct);
1072 return MMSYSERR_INVALPARAM;
1076 return MMSYSERR_BADDEVICEID;
1078 if (line < 0 || line >= mmixer->chans)
1080 WARN("Invalid line id: %d not in range of 0-%d\n", line, mmixer->chans-1);
1081 return MMSYSERR_INVALPARAM;
1084 if (!mmixer->controls[ctrl].enabled)
1086 WARN("Control %d not enabled\n", ctrl);
1087 return MIXERR_INVALCONTROL;
1090 ct = &mmixer->controls[ctrl];
1091 elem = mmixer->lines[line].elem;
1092 flags &= MIXER_SETCONTROLDETAILSF_QUERYMASK;
1095 case MIXER_SETCONTROLDETAILSF_VALUE:
1096 TRACE("MIXER_SETCONTROLDETAILSF_VALUE (%d)\n", ctrl);
1100 WARN("Unknown flag (%08lx)\n", flags);
1101 return MMSYSERR_INVALPARAM;
1104 switch (ct->c.dwControlType)
1106 case MIXERCONTROL_CONTROLTYPE_VOLUME:
1108 long min = 0, max = 0;
1110 LPMIXERCONTROLDETAILS_UNSIGNED mcdu;
1111 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
1113 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_UNSIGNED))
1115 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1116 return MMSYSERR_INVALPARAM;
1119 if (mctrld->cChannels != 1 && mmixer->lines[line].chans != mctrld->cChannels)
1121 WARN("Unsupported cChannels (%d instead of %d)\n", mctrld->cChannels, mmixer->lines[line].chans);
1122 return MMSYSERR_INVALPARAM;
1125 TRACE("%s MIXERCONTROLDETAILS_UNSIGNED[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1126 mcdu = (LPMIXERCONTROLDETAILS_UNSIGNED)mctrld->paDetails;
1128 for (chn=0; chn<mctrld->cChannels;++chn)
1130 TRACE("Chan %d value %d\n", chn, mcdu[chn].dwValue);
1133 /* There isn't always a capture volume, so in that case change playback volume */
1134 if (mmixer->lines[line].capt && snd_mixer_selem_has_capture_volume(elem))
1136 snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
1138 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1139 if (snd_mixer_selem_has_capture_channel(elem, chn))
1141 snd_mixer_selem_set_capture_volume(elem, chn, min + normalized(mcdu->dwValue, 65535, max));
1142 if (mctrld->cChannels != 1)
1148 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
1150 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1151 if (snd_mixer_selem_has_playback_channel(elem, chn))
1153 snd_mixer_selem_set_playback_volume(elem, chn, min + normalized(mcdu->dwValue, 65535, max));
1154 if (mctrld->cChannels != 1)
1161 case MIXERCONTROL_CONTROLTYPE_MUTE:
1162 case MIXERCONTROL_CONTROLTYPE_ONOFF:
1164 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1166 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1168 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1169 return MMSYSERR_INVALPARAM;
1172 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1174 mcdb = (LPMIXERCONTROLDETAILS_BOOLEAN)mctrld->paDetails;
1175 if (line == 1) /* Mute/unmute capturing */
1176 for (i = 0; i <= SND_MIXER_SCHN_LAST; ++i)
1178 if (snd_mixer_selem_has_capture_channel(elem, i))
1179 snd_mixer_selem_set_capture_switch(elem, i, !mcdb->fValue);
1182 for (i = 0; i <= SND_MIXER_SCHN_LAST; ++i)
1183 if (snd_mixer_selem_has_playback_channel(elem, i))
1184 snd_mixer_selem_set_playback_switch(elem, i, !mcdb->fValue);
1188 case MIXERCONTROL_CONTROLTYPE_MIXER:
1189 case MIXERCONTROL_CONTROLTYPE_MUX:
1191 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1193 int didone = 0, canone = (ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MUX);
1195 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1197 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1198 return MMSYSERR_INVALPARAM;
1201 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1202 mcdb = (LPMIXERCONTROLDETAILS_BOOLEAN)mctrld->paDetails;
1204 for (x=i=0; x < mmixer->chans; ++x)
1205 if (line != x && mmixer->lines[x].dst == line)
1207 TRACE("fVal[%i] (%s) = %i\n", i, debugstr_w(mmixer->lines[x].name), mcdb[i].fValue);
1208 if (i >= mctrld->u.cMultipleItems)
1210 TRACE("Too many items to fit, overflowing\n");
1211 return MIXERR_INVALVALUE;
1213 if (mcdb[i].fValue && canone && didone)
1215 TRACE("Nice try, but it's not going to work\n");
1216 elem_callback(mmixer->lines[1].elem, SND_CTL_EVENT_MASK_VALUE);
1217 return MIXERR_INVALVALUE;
1224 if (canone && !didone)
1226 TRACE("Nice try, this is not going to work either\n");
1227 elem_callback(mmixer->lines[1].elem, SND_CTL_EVENT_MASK_VALUE);
1228 return MIXERR_INVALVALUE;
1231 for (x = i = 0; x<mmixer->chans; ++x)
1232 if (line != x && mmixer->lines[x].dst == line)
1235 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1237 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1239 snd_mixer_selem_set_capture_switch(mmixer->lines[x].elem, chn, mcdb[i].fValue);
1244 /* If it's a MUX, it means that only 1 channel can be selected
1245 * and the other channels are unselected
1247 * For MIXER multiple sources are allowed, so unselect here
1252 for (x = i = 0; x<mmixer->chans; ++x)
1253 if (line != x && mmixer->lines[x].dst == line)
1255 if (!mcdb[i].fValue)
1256 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1258 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1260 snd_mixer_selem_set_capture_switch(mmixer->lines[x].elem, chn, mcdb[i].fValue);
1267 FIXME("Unhandled type %s\n", getControlType(ct->c.dwControlType));
1268 return MMSYSERR_INVALPARAM;
1270 return MMSYSERR_NOERROR;
1273 /* Here we give info over the source/dest line given by dwSource+dwDest or dwDest, respectively
1274 * It is also possible that a line is found by componenttype or target type, latter is not implemented yet
1275 * Most important values returned in struct:
1278 * line control count
1279 * amount of channels
1281 static DWORD MIX_GetLineInfo(UINT wDevID, LPMIXERLINEW Ml, DWORD_PTR flags)
1283 DWORD_PTR qf = flags & MIXER_GETLINEINFOF_QUERYMASK;
1284 mixer *mmixer = MIX_GetMix(wDevID);
1291 return MMSYSERR_INVALPARAM;
1296 WARN("Device %u not found\n", wDevID);
1297 return MMSYSERR_BADDEVICEID;
1300 if (Ml->cbStruct != sizeof(*Ml))
1302 WARN("invalid parameter: Ml->cbStruct = %d\n", Ml->cbStruct);
1303 return MMSYSERR_INVALPARAM;
1306 Ml->fdwLine = MIXERLINE_LINEF_ACTIVE;
1311 case MIXER_GETLINEINFOF_COMPONENTTYPE:
1313 Ml->dwLineID = 0xFFFF;
1314 for (idx = 0; idx < mmixer->chans; ++idx)
1315 if (mmixer->lines[idx].component == Ml->dwComponentType)
1320 if (Ml->dwLineID == 0xFFFF)
1321 return MMSYSERR_KEYNOTFOUND;
1322 /* Now that we have lineid, fallback to lineid*/
1325 case MIXER_GETLINEINFOF_LINEID:
1326 if (Ml->dwLineID < 0 || Ml->dwLineID >= mmixer->chans)
1327 return MIXERR_INVALLINE;
1329 TRACE("MIXER_GETLINEINFOF_LINEID %d\n", Ml->dwLineID);
1330 Ml->dwDestination = mmixer->lines[Ml->dwLineID].dst;
1332 if (Ml->dwDestination != Ml->dwLineID)
1334 Ml->dwSource = getsrcfromline(mmixer, Ml->dwLineID);
1335 Ml->cConnections = 1;
1339 Ml->cConnections = getsrccntfromchan(mmixer, Ml->dwLineID);
1340 Ml->dwSource = 0xFFFFFFFF;
1342 TRACE("Connections %d, source %d\n", Ml->cConnections, Ml->dwSource);
1345 case MIXER_GETLINEINFOF_DESTINATION:
1346 if (Ml->dwDestination < 0 || Ml->dwDestination >= mmixer->dests)
1348 WARN("dest %d out of bounds\n", Ml->dwDestination);
1349 return MIXERR_INVALLINE;
1352 Ml->dwLineID = Ml->dwDestination;
1353 Ml->cConnections = getsrccntfromchan(mmixer, Ml->dwLineID);
1354 Ml->dwSource = 0xFFFFFFFF;
1357 case MIXER_GETLINEINFOF_SOURCE:
1358 if (Ml->dwDestination < 0 || Ml->dwDestination >= mmixer->dests)
1360 WARN("dest %d for source out of bounds\n", Ml->dwDestination);
1361 return MIXERR_INVALLINE;
1364 if (Ml->dwSource < 0 || Ml->dwSource >= getsrccntfromchan(mmixer, Ml->dwDestination))
1366 WARN("src %d out of bounds\n", Ml->dwSource);
1367 return MIXERR_INVALLINE;
1370 Ml->dwLineID = getsrclinefromchan(mmixer, Ml->dwDestination, Ml->dwSource);
1371 Ml->cConnections = 1;
1374 case MIXER_GETLINEINFOF_TARGETTYPE:
1375 FIXME("TODO: TARGETTYPE, stub\n");
1376 return MMSYSERR_INVALPARAM;
1379 FIXME("Unknown query flag: %08lx\n", qf);
1380 return MMSYSERR_INVALPARAM;
1383 if (Ml->dwLineID >= mmixer->dests)
1384 Ml->fdwLine |= MIXERLINE_LINEF_SOURCE;
1386 mline = &mmixer->lines[Ml->dwLineID];
1387 Ml->dwComponentType = mline->component;
1388 Ml->cChannels = mmixer->lines[Ml->dwLineID].chans;
1391 for (i=CONTROLSPERLINE*Ml->dwLineID;i<CONTROLSPERLINE*(Ml->dwLineID+1); ++i)
1392 if (mmixer->controls[i].enabled)
1395 lstrcpynW(Ml->szShortName, mmixer->lines[Ml->dwLineID].name, sizeof(Ml->szShortName)/sizeof(WCHAR));
1396 lstrcpynW(Ml->szName, mmixer->lines[Ml->dwLineID].name, sizeof(Ml->szName)/sizeof(WCHAR));
1398 Ml->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
1400 Ml->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
1401 Ml->Target.dwDeviceID = 0xFFFFFFFF;
1402 Ml->Target.wMid = WINE_MIXER_MANUF_ID;
1403 Ml->Target.wPid = WINE_MIXER_PRODUCT_ID;
1404 Ml->Target.vDriverVersion = WINE_MIXER_VERSION;
1405 lstrcpynW(Ml->Target.szPname, mmixer->mixername, sizeof(Ml->Target.szPname)/sizeof(WCHAR));
1406 return MMSYSERR_NOERROR;
1409 /* Get the controls that belong to a certain line, either all or 1 */
1410 static DWORD MIX_GetLineControls(UINT wDevID, LPMIXERLINECONTROLSW mlc, DWORD_PTR flags)
1412 mixer *mmixer = MIX_GetMix(wDevID);
1416 if (!mlc || mlc->cbStruct != sizeof(*mlc))
1418 WARN("Invalid mlc %p, cbStruct: %d\n", mlc, (!mlc ? -1 : mlc->cbStruct));
1419 return MMSYSERR_INVALPARAM;
1422 if (mlc->cbmxctrl != sizeof(MIXERCONTROLW))
1424 WARN("cbmxctrl %d\n", mlc->cbmxctrl);
1425 return MMSYSERR_INVALPARAM;
1429 return MMSYSERR_BADDEVICEID;
1431 flags &= MIXER_GETLINECONTROLSF_QUERYMASK;
1433 if (flags == MIXER_GETLINECONTROLSF_ONEBYID)
1434 mlc->dwLineID = mlc->u.dwControlID / CONTROLSPERLINE;
1436 if (mlc->dwLineID < 0 || mlc->dwLineID >= mmixer->chans)
1438 TRACE("Invalid dwLineID %d\n", mlc->dwLineID);
1439 return MIXERR_INVALLINE;
1444 case MIXER_GETLINECONTROLSF_ALL:
1445 TRACE("line=%08x MIXER_GETLINECONTROLSF_ALL (%d)\n", mlc->dwLineID, mlc->cControls);
1446 for (i = 0; i < CONTROLSPERLINE; ++i)
1447 if (mmixer->controls[i+mlc->dwLineID * CONTROLSPERLINE].enabled)
1449 memcpy(&mlc->pamxctrl[j], &mmixer->controls[i+mlc->dwLineID * CONTROLSPERLINE].c, sizeof(MIXERCONTROLW));
1450 TRACE("Added %s (%s)\n", debugstr_w(mlc->pamxctrl[j].szShortName), debugstr_w(mlc->pamxctrl[j].szName));
1452 if (j > mlc->cControls)
1454 WARN("invalid parameter\n");
1455 return MMSYSERR_INVALPARAM;
1459 if (!j || mlc->cControls > j)
1461 WARN("invalid parameter\n");
1462 return MMSYSERR_INVALPARAM;
1465 case MIXER_GETLINECONTROLSF_ONEBYID:
1466 TRACE("line=%08x MIXER_GETLINECONTROLSF_ONEBYID (%x)\n", mlc->dwLineID, mlc->u.dwControlID);
1468 if (!mmixer->controls[mlc->u.dwControlID].enabled)
1469 return MIXERR_INVALCONTROL;
1471 mlc->pamxctrl[0] = mmixer->controls[mlc->u.dwControlID].c;
1473 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
1474 TRACE("line=%08x MIXER_GETLINECONTROLSF_ONEBYTYPE (%s)\n", mlc->dwLineID, getControlType(mlc->u.dwControlType));
1476 ct = mlc->u.dwControlType & MIXERCONTROL_CT_CLASS_MASK;
1477 for (i = 0; i <= CONTROLSPERLINE; ++i)
1479 const int ofs = i+mlc->dwLineID*CONTROLSPERLINE;
1480 if (i == CONTROLSPERLINE)
1482 WARN("invalid parameter: control %s not found\n", getControlType(mlc->u.dwControlType));
1483 return MIXERR_INVALCONTROL;
1485 if (mmixer->controls[ofs].enabled && (mmixer->controls[ofs].c.dwControlType & MIXERCONTROL_CT_CLASS_MASK) == ct)
1487 mlc->pamxctrl[0] = mmixer->controls[ofs].c;
1493 FIXME("Unknown flag %08lx\n", flags & MIXER_GETLINECONTROLSF_QUERYMASK);
1494 return MMSYSERR_INVALPARAM;
1497 return MMSYSERR_NOERROR;
1500 #endif /*HAVE_ALSA*/
1502 /**************************************************************************
1503 * mxdMessage (WINEALSA.3)
1505 DWORD WINAPI ALSA_mxdMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1506 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1510 TRACE("(%04X, %s, %08lX, %08lX, %08lX);\n", wDevID, getMessage(wMsg),
1511 dwUser, dwParam1, dwParam2);
1515 case DRVM_INIT: ALSA_MixerInit(); ret = MMSYSERR_NOERROR; break;
1516 case DRVM_EXIT: ALSA_MixerExit(); ret = MMSYSERR_NOERROR; break;
1517 /* All taken care of by driver initialisation */
1518 /* Unimplemented, and not needed */
1521 ret = MMSYSERR_NOERROR; break;
1524 ret = MIX_Open(wDevID, (LPMIXEROPENDESC) dwParam1, dwParam2); break;
1527 ret = MIX_Close(wDevID); break;
1529 case MXDM_GETDEVCAPS:
1530 ret = MIX_GetDevCaps(wDevID, (LPMIXERCAPS2W)dwParam1, dwParam2); break;
1532 case MXDM_GETLINEINFO:
1533 ret = MIX_GetLineInfo(wDevID, (LPMIXERLINEW)dwParam1, dwParam2); break;
1535 case MXDM_GETLINECONTROLS:
1536 ret = MIX_GetLineControls(wDevID, (LPMIXERLINECONTROLSW)dwParam1, dwParam2); break;
1538 case MXDM_GETCONTROLDETAILS:
1539 ret = MIX_GetControlDetails(wDevID, (LPMIXERCONTROLDETAILS)dwParam1, dwParam2); break;
1541 case MXDM_SETCONTROLDETAILS:
1542 ret = MIX_SetControlDetails(wDevID, (LPMIXERCONTROLDETAILS)dwParam1, dwParam2); break;
1544 case MXDM_GETNUMDEVS:
1548 WARN("unknown message %s!\n", getMessage(wMsg));
1549 return MMSYSERR_NOTSUPPORTED;
1552 TRACE("Returning %08X\n", ret);
1555 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1557 return MMSYSERR_NOTENABLED;
1558 #endif /*HAVE_ALSA*/