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
50 #include "wine/unicode.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(mixer);
57 #define WINE_MIXER_MANUF_ID 0xAA
58 #define WINE_MIXER_PRODUCT_ID 0x55
59 #define WINE_MIXER_VERSION 0x0100
62 * In windows it seems to be required for all controls to have a volume switch
63 * In alsa that's optional
65 * I assume for playback controls, that there is always a playback volume switch available
68 * For capture controls, it is needed that there is a capture switch and a volume switch,
69 * It doesn't matter whether it is a playback volume switch or a capture volume switch.
70 * The code will first try to get/adjust capture volume, if that fails it tries playback volume
71 * It is not pretty, but under my 3 test cards it seems that there is no other choice:
72 * Most capture controls don't have a capture volume setting
74 * MUX means that only capture source can be exclusively selected,
75 * MIXER means that multiple sources can be selected simultaneously.
78 static const char * getMessage(UINT uMsg)
81 #define MSG_TO_STR(x) case x: return #x;
83 MSG_TO_STR(DRVM_INIT);
84 MSG_TO_STR(DRVM_EXIT);
85 MSG_TO_STR(DRVM_ENABLE);
86 MSG_TO_STR(DRVM_DISABLE);
87 MSG_TO_STR(MXDM_GETDEVCAPS);
88 MSG_TO_STR(MXDM_GETLINEINFO);
89 MSG_TO_STR(MXDM_GETNUMDEVS);
90 MSG_TO_STR(MXDM_OPEN);
91 MSG_TO_STR(MXDM_CLOSE);
92 MSG_TO_STR(MXDM_GETLINECONTROLS);
93 MSG_TO_STR(MXDM_GETCONTROLDETAILS);
94 MSG_TO_STR(MXDM_SETCONTROLDETAILS);
98 sprintf(str, "UNKNOWN(%08x)", uMsg);
102 static const char * getControlType(DWORD dwControlType)
104 #define TYPE_TO_STR(x) case x: return #x;
105 switch (dwControlType) {
106 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_CUSTOM);
107 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BOOLEANMETER);
108 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SIGNEDMETER);
109 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PEAKMETER);
110 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_UNSIGNEDMETER);
111 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BOOLEAN);
112 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_ONOFF);
113 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MUTE);
114 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MONO);
115 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_LOUDNESS);
116 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_STEREOENH);
117 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BASS_BOOST);
118 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BUTTON);
119 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_DECIBELS);
120 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SIGNED);
121 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_UNSIGNED);
122 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PERCENT);
123 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SLIDER);
124 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PAN);
125 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_QSOUNDPAN);
126 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_FADER);
127 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_VOLUME);
128 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BASS);
129 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_TREBLE);
130 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_EQUALIZER);
131 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SINGLESELECT);
132 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MUX);
133 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MULTIPLESELECT);
134 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MIXER);
135 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MICROTIME);
136 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MILLITIME);
139 return wine_dbg_sprintf("UNKNOWN(%08x)", dwControlType);
142 /* A simple declaration of a line control
143 * These are each of the channels that show up
145 typedef struct line {
146 /* Name we present to outside world */
147 WCHAR name[MAXPNAMELEN];
153 snd_mixer_elem_t *elem;
156 /* A control structure, with toggle enabled switch
157 * Control structures control volume, muted, which capture source
159 typedef struct control {
168 WCHAR mixername[MAXPNAMELEN];
171 LPDRVCALLBACK callback;
172 DWORD_PTR callbackpriv;
179 #define MAX_MIXERS 32
180 #define CONTROLSPERLINE 3
184 static int cards = 0;
185 static mixer mixdev[MAX_MIXERS];
186 static HANDLE thread;
187 static int elem_callback(snd_mixer_elem_t *elem, unsigned int mask);
188 static DWORD WINAPI ALSA_MixerPollThread(LPVOID lParam);
189 static CRITICAL_SECTION elem_crst;
190 static int msg_pipe[2];
193 /* found channel names in alsa lib, alsa api doesn't have another way for this
194 * map name -> componenttype, worst case we get a wrong componenttype which is
198 static const struct mixerlinetype {
199 const char *name; DWORD cmpt;
201 { "Master", MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, },
202 { "Capture", MIXERLINE_COMPONENTTYPE_DST_WAVEIN, },
203 { "PCM", MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT, },
204 { "PC Speaker", MIXERLINE_COMPONENTTYPE_SRC_PCSPEAKER, },
205 { "Synth", MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER, },
206 { "Headphone", MIXERLINE_COMPONENTTYPE_DST_HEADPHONES, },
207 { "Mic", MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, },
208 { "Aux", MIXERLINE_COMPONENTTYPE_SRC_UNDEFINED, },
209 { "CD", MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC, },
210 { "Line", MIXERLINE_COMPONENTTYPE_SRC_LINE, },
211 { "Phone", MIXERLINE_COMPONENTTYPE_SRC_TELEPHONE, },
212 { "Digital", MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, },
213 { "Front Mic", MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, },
216 /* Map name to MIXERLINE_COMPONENTTYPE_XXX */
217 static int getcomponenttype(const char *name)
220 for (x=0; x< sizeof(converttable)/sizeof(converttable[0]); ++x)
221 if (!strcasecmp(name, converttable[x].name))
223 TRACE("%d -> %s\n", x, name);
224 return converttable[x].cmpt;
226 WARN("Unknown mixer name %s, probably harmless\n", name);
227 return MIXERLINE_COMPONENTTYPE_SRC_UNDEFINED;
230 /* Is this control suited for showing up? */
231 static int blacklisted(snd_mixer_elem_t *elem)
233 const char *name = snd_mixer_selem_get_name(elem);
236 if (!snd_mixer_selem_has_playback_volume(elem) &&
237 !snd_mixer_selem_has_capture_volume(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) &&
375 (snd_mixer_selem_has_capture_volume(elem) || comp != MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE))
377 (++mline)->component = comp;
378 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, mline->name, MAXPNAMELEN);
379 mline->capt = mline->dst = 0;
381 mline->chans = chans(mmixer, elem, 0);
386 if (capt && (snd_mixer_selem_has_capture_volume(elem) || comp == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE))
388 (++mline)->component = comp;
389 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, mline->name, MAXPNAMELEN);
390 mline->capt = mline->dst = 1;
392 mline->chans = chans(mmixer, elem, 1);
395 snd_mixer_elem_set_callback(elem, &elem_callback);
396 snd_mixer_elem_set_callback_private(elem, mmixer);
400 /* Windows api wants to have a 'master' device to which all slaves are attached
401 * There are 2 ones in this code:
402 * - 'Master', fall back to 'Headphone' if unavailable, and if that's not available 'PCM'
404 * Capture might not always be available, so should be prepared to be without if needed
407 static void ALSA_MixerInit(void)
410 snd_ctl_card_info_t *info;
412 info = HeapAlloc( GetProcessHeap(), 0, snd_ctl_card_info_sizeof());
413 for (x = 0; x < MAX_MIXERS; ++x)
415 int card, err, capcontrols = 0;
416 char cardind[6], cardname[10];
419 snd_mixer_elem_t *elem, *mastelem = NULL, *headelem = NULL, *captelem = NULL, *pcmelem = NULL;
421 memset(info, 0, snd_ctl_card_info_sizeof());
422 memset(&mixdev[mixnum], 0, sizeof(*mixdev));
423 snprintf(cardind, sizeof(cardind), "%d", x);
424 card = snd_card_get_index(cardind);
428 snprintf(cardname, sizeof(cardname), "hw:%d", card);
430 err = snd_ctl_open(&ctl, cardname, 0);
433 WARN("Cannot open card: %s\n", snd_strerror(err));
437 err = snd_ctl_card_info(ctl, info);
440 WARN("Cannot get card info: %s\n", snd_strerror(err));
445 MultiByteToWideChar(CP_UNIXCP, 0, snd_ctl_card_info_get_name(info), -1, mixdev[mixnum].mixername, sizeof(mixdev[mixnum].mixername)/sizeof(WCHAR));
448 err = snd_mixer_open(&mixdev[mixnum].mix, 0);
451 WARN("Error occurred opening mixer: %s\n", snd_strerror(err));
455 err = snd_mixer_attach(mixdev[mixnum].mix, cardname);
459 err = snd_mixer_selem_register(mixdev[mixnum].mix, NULL, NULL);
463 err = snd_mixer_load(mixdev[mixnum].mix);
467 /* First, lets see what's available..
468 * If there are multiple Master or Captures, all except 1 will be added as slaves
470 for (elem = snd_mixer_first_elem(mixdev[mixnum].mix); elem; elem = snd_mixer_elem_next(elem))
471 if (!strcasecmp(snd_mixer_selem_get_name(elem), "Master") && !mastelem)
473 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "Capture") && !captelem)
475 else if (!blacklisted(elem))
477 DWORD comp = getcomponenttype(snd_mixer_selem_get_name(elem));
480 /* Work around buggy drivers: Make this a capture control if the name is recognised as a microphone */
481 if (snd_mixer_selem_has_capture_volume(elem))
483 else if (comp == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE)
489 if (!skip && snd_mixer_selem_has_playback_volume(elem))
491 if (!strcasecmp(snd_mixer_selem_get_name(elem), "Headphone") && !headelem)
493 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "PCM") && !pcmelem)
496 ++(mixdev[mixnum].chans);
500 /* Add master channel, uncounted channels and an extra for capture */
501 mixdev[mixnum].chans += !!mastelem + !!headelem + !!pcmelem + 1;
503 /* If there is only 'Capture' and 'Master', this device is not worth it */
504 if (mixdev[mixnum].chans == 2)
506 WARN("No channels found, skipping device!\n");
510 /* Master element can't have a capture control in this code, so
511 * if Headphone or PCM is promoted to master, unset its capture control */
512 if (headelem && !mastelem)
514 /* Using 'Headphone' as master device */
516 capcontrols -= !!snd_mixer_selem_has_capture_switch(mastelem);
518 else if (pcmelem && !mastelem)
520 /* Use 'PCM' as master device */
522 capcontrols -= !!snd_mixer_selem_has_capture_switch(mastelem);
526 /* If there is nothing sensible that can act as 'Master' control, something is wrong */
527 FIXME("No master control found on %s, disabling mixer\n", snd_ctl_card_info_get_name(info));
531 if (!captelem || !capcontrols)
533 /* Can't enable capture, so disabling it
534 * Note: capture control will still exist because
535 * dwLineID 0 and 1 are reserved for Master and Capture
537 WARN("No use enabling capture part of mixer, capture control found: %s, amount of capture controls: %d\n",
538 (!captelem ? "no" : "yes"), capcontrols);
540 mixdev[mixnum].dests = 1;
544 mixdev[mixnum].chans += capcontrols;
545 mixdev[mixnum].dests = 2;
548 mixdev[mixnum].lines = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(line) * mixdev[mixnum].chans);
549 mixdev[mixnum].controls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(control) * CONTROLSPERLINE*mixdev[mixnum].chans);
551 if (!mixdev[mixnum].lines || !mixdev[mixnum].controls)
554 filllines(&mixdev[mixnum], mastelem, captelem, capcontrols);
555 fillcontrols(&mixdev[mixnum]);
557 TRACE("%s: Amount of controls: %i/%i, name: %s\n", cardname, mixdev[mixnum].dests, mixdev[mixnum].chans, debugstr_w(mixdev[mixnum].mixername));
562 WARN("Error occurred initialising mixer: %s\n", snd_strerror(err));
564 HeapFree(GetProcessHeap(), 0, mixdev[mixnum].lines);
565 HeapFree(GetProcessHeap(), 0, mixdev[mixnum].controls);
566 snd_mixer_close(mixdev[mixnum].mix);
569 HeapFree( GetProcessHeap(), 0, info );
571 /* There is no trouble with already assigning callbacks without initialising critsect:
572 * Callbacks only occur when snd_mixer_handle_events is called (only happens in thread)
574 InitializeCriticalSection(&elem_crst);
575 elem_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_MIXER.elem_crst");
579 static void ALSA_MixerExit(void)
585 WARN("Callback thread still alive, terminating uncleanly, refcnt: %d\n", refcnt);
586 /* Least we can do is making sure we're not in 'foreign' code */
587 EnterCriticalSection(&elem_crst);
588 TerminateThread(thread, 1);
590 LeaveCriticalSection(&elem_crst);
593 TRACE("Cleaning up\n");
595 elem_crst.DebugInfo->Spare[0] = 0;
596 DeleteCriticalSection(&elem_crst);
597 for (x = 0; x < cards; ++x)
599 snd_mixer_close(mixdev[x].mix);
600 HeapFree(GetProcessHeap(), 0, mixdev[x].lines);
601 HeapFree(GetProcessHeap(), 0, mixdev[x].controls);
606 static mixer* MIX_GetMix(UINT wDevID)
612 WARN("Invalid mixer id: %d\n", wDevID);
616 mmixer = &mixdev[wDevID];
620 /* Since alsa doesn't tell what exactly changed, just assume all affected controls changed */
621 static int elem_callback(snd_mixer_elem_t *elem, unsigned int type)
623 mixer *mmixer = snd_mixer_elem_get_callback_private(elem);
625 BOOL captchanged = 0;
627 if (type != SND_CTL_EVENT_MASK_VALUE)
632 EnterCriticalSection(&elem_crst);
634 if (!mmixer->callback)
637 for (x=0; x<mmixer->chans; ++x)
639 const int ofs = CONTROLSPERLINE*x;
640 if (elem != mmixer->lines[x].elem)
643 if (mmixer->lines[x].capt)
646 TRACE("Found changed control %s\n", debugstr_w(mmixer->lines[x].name));
647 mmixer->callback(mmixer->hmx, MM_MIXM_LINE_CHANGE, mmixer->callbackpriv, x, 0);
648 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, ofs, 0);
650 if (mmixer->controls[ofs+OFS_MUTE].enabled)
651 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, ofs+OFS_MUTE, 0);
654 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, CONTROLSPERLINE+OFS_MUX, 0);
657 LeaveCriticalSection(&elem_crst);
662 static DWORD WINAPI ALSA_MixerPollThread(LPVOID lParam)
664 struct pollfd *pfds = NULL;
665 int x, y, err, mcnt, count = 1;
667 TRACE("%p\n", lParam);
669 for (x = 0; x < cards; ++x)
670 count += snd_mixer_poll_descriptors_count(mixdev[x].mix);
672 TRACE("Counted %d descriptors\n", count);
673 pfds = HeapAlloc(GetProcessHeap(), 0, count * sizeof(struct pollfd));
677 WARN("Out of memory\n");
681 pfds[0].fd = msg_pipe[0];
682 pfds[0].events = POLLIN;
685 for (x = 0; x < cards; ++x)
686 y += snd_mixer_poll_descriptors(mixdev[x].mix, &pfds[y], count - y);
688 while ((err = poll(pfds, (unsigned int) count, -1)) >= 0 || errno == EINTR || errno == EAGAIN)
690 if (pfds[0].revents & POLLIN)
694 for (x = y = 0; x < cards; ++x)
696 int j, max = snd_mixer_poll_descriptors_count(mixdev[x].mix);
697 for (j = 0; j < max; ++j)
698 if (pfds[mcnt+j].revents)
700 y += snd_mixer_handle_events(mixdev[x].mix);
706 TRACE("Handled %d events\n", y);
710 TRACE("Shutting down\n");
711 HeapFree(GetProcessHeap(), 0, pfds);
713 y = read(msg_pipe[0], &x, sizeof(x));
719 static DWORD MIX_Open(UINT wDevID, LPMIXEROPENDESC desc, DWORD_PTR flags)
721 mixer *mmixer = MIX_GetMix(wDevID);
723 return MMSYSERR_BADDEVICEID;
725 flags &= CALLBACK_TYPEMASK;
731 case CALLBACK_FUNCTION:
735 FIXME("Unhandled callback type: %08lx\n", flags & CALLBACK_TYPEMASK);
736 return MIXERR_INVALVALUE;
739 mmixer->callback = (LPDRVCALLBACK)desc->dwCallback;
740 mmixer->callbackpriv = desc->dwInstance;
741 mmixer->hmx = (HDRVR)desc->hmx;
744 if (InterlockedIncrement(&refcnt) == 1)
746 if (pipe(msg_pipe) >= 0)
748 thread = CreateThread(NULL, 0, ALSA_MixerPollThread, NULL, 0, NULL);
753 msg_pipe[0] = msg_pipe[1] = -1;
757 msg_pipe[0] = msg_pipe[1] = -1;
760 return MMSYSERR_NOERROR;
763 static DWORD MIX_Close(UINT wDevID)
766 mixer *mmixer = MIX_GetMix(wDevID);
768 return MMSYSERR_BADDEVICEID;
770 EnterCriticalSection(&elem_crst);
771 mmixer->callback = 0;
772 LeaveCriticalSection(&elem_crst);
774 if (!InterlockedDecrement(&refcnt))
776 if (write(msg_pipe[1], &x, sizeof(x)) > 0)
778 TRACE("Shutting down thread...\n");
779 WaitForSingleObject(thread, INFINITE);
784 return MMSYSERR_NOERROR;
787 static DWORD MIX_GetDevCaps(UINT wDevID, LPMIXERCAPS2W caps, DWORD_PTR parm2)
789 mixer *mmixer = MIX_GetMix(wDevID);
793 return MMSYSERR_INVALPARAM;
796 return MMSYSERR_BADDEVICEID;
798 memset(&capsW, 0, sizeof(MIXERCAPS2W));
800 capsW.wMid = WINE_MIXER_MANUF_ID;
801 capsW.wPid = WINE_MIXER_PRODUCT_ID;
802 capsW.vDriverVersion = WINE_MIXER_VERSION;
804 lstrcpynW(capsW.szPname, mmixer->mixername, sizeof(capsW.szPname)/sizeof(WCHAR));
805 capsW.cDestinations = mmixer->dests;
806 memcpy(caps, &capsW, min(parm2, sizeof(capsW)));
807 return MMSYSERR_NOERROR;
810 /* convert win32 volume to alsa volume, and vice versa */
811 static INT normalized(INT value, INT prevmax, INT nextmax)
813 int ret = MulDiv(value, nextmax, prevmax);
815 /* Have to stay in range */
816 TRACE("%d/%d -> %d/%d\n", value, prevmax, ret, nextmax);
825 /* get amount of sources for dest */
826 static int getsrccntfromchan(mixer *mmixer, int dad)
830 for (i=0; i<mmixer->chans; ++i)
831 if (i != dad && mmixer->lines[i].dst == dad)
836 FIXME("No src found for %i (%s)?\n", dad, debugstr_w(mmixer->lines[dad].name));
840 /* find lineid for source 'num' with dest 'dad' */
841 static int getsrclinefromchan(mixer *mmixer, int dad, int num)
844 for (i=0; i<mmixer->chans; ++i)
845 if (i != dad && mmixer->lines[i].dst == dad)
851 WARN("No src found for src %i from dest %i\n", num, dad);
855 /* get the source number belonging to line */
856 static int getsrcfromline(mixer *mmixer, int line)
858 int i, j=0, dad = mmixer->lines[line].dst;
860 for (i=0; i<mmixer->chans; ++i)
861 if (i != dad && mmixer->lines[i].dst == dad)
867 WARN("No src found for line %i with dad %i\n", line, dad);
871 /* Get volume/muted/capture channel */
872 static DWORD MIX_GetControlDetails(UINT wDevID, LPMIXERCONTROLDETAILS mctrld, DWORD_PTR flags)
874 mixer *mmixer = MIX_GetMix(wDevID);
880 return MMSYSERR_INVALPARAM;
882 ctrl = mctrld->dwControlID;
883 line = ctrl/CONTROLSPERLINE;
885 if (mctrld->cbStruct != sizeof(*mctrld))
886 return MMSYSERR_INVALPARAM;
889 return MMSYSERR_BADDEVICEID;
891 if (line >= mmixer->chans || !mmixer->controls[ctrl].enabled)
892 return MIXERR_INVALCONTROL;
894 ct = &mmixer->controls[ctrl];
896 flags &= MIXER_GETCONTROLDETAILSF_QUERYMASK;
899 case MIXER_GETCONTROLDETAILSF_VALUE:
900 TRACE("MIXER_GETCONTROLDETAILSF_VALUE (%d/%d)\n", ctrl, line);
901 switch (ct->c.dwControlType)
903 case MIXERCONTROL_CONTROLTYPE_VOLUME:
905 long min = 0, max = 0, vol = 0;
907 LPMIXERCONTROLDETAILS_UNSIGNED mcdu;
908 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
910 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_UNSIGNED))
912 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
913 return MMSYSERR_INVALPARAM;
916 TRACE("%s MIXERCONTROLDETAILS_UNSIGNED[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
918 mcdu = mctrld->paDetails;
920 if (mctrld->cChannels != 1 && mmixer->lines[line].chans != mctrld->cChannels)
922 WARN("Unsupported cChannels (%d instead of %d)\n", mctrld->cChannels, mmixer->lines[line].chans);
923 return MMSYSERR_INVALPARAM;
926 if (mmixer->lines[line].capt && snd_mixer_selem_has_capture_volume(elem)) {
927 snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
928 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
929 if (snd_mixer_selem_has_capture_channel(elem, chn))
931 snd_mixer_selem_get_capture_volume(elem, chn, &vol);
932 mcdu->dwValue = normalized(vol - min, max, 65535);
933 if (mctrld->cChannels == 1)
938 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
940 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
941 if (snd_mixer_selem_has_playback_channel(elem, chn))
943 snd_mixer_selem_get_playback_volume(elem, chn, &vol);
944 mcdu->dwValue = normalized(vol - min, max, 65535);
945 if (mctrld->cChannels == 1)
951 return MMSYSERR_NOERROR;
954 case MIXERCONTROL_CONTROLTYPE_ONOFF:
955 case MIXERCONTROL_CONTROLTYPE_MUTE:
957 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
959 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
961 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
963 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
964 return MMSYSERR_INVALPARAM;
967 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
969 mcdb = mctrld->paDetails;
972 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
974 if (!snd_mixer_selem_has_capture_channel(elem, chn))
976 snd_mixer_selem_get_capture_switch(elem, chn, &ival);
980 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
982 if (!snd_mixer_selem_has_playback_channel(elem, chn))
984 snd_mixer_selem_get_playback_switch(elem, chn, &ival);
988 if (chn > SND_MIXER_SCHN_LAST)
990 TRACE("can't find active channel\n");
991 return MMSYSERR_INVALPARAM; /* fixme: what's right error? */
994 mcdb->fValue = !ival;
995 TRACE("=> %s\n", mcdb->fValue ? "on" : "off");
996 return MMSYSERR_NOERROR;
998 case MIXERCONTROL_CONTROLTYPE_MIXER:
999 case MIXERCONTROL_CONTROLTYPE_MUX:
1001 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1002 int x, i=0, ival = 0, chn;
1004 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1006 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1007 return MMSYSERR_INVALPARAM;
1010 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1012 mcdb = mctrld->paDetails;
1014 for (x = 0; x<mmixer->chans; ++x)
1015 if (line != x && mmixer->lines[x].dst == line)
1018 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1020 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1022 snd_mixer_selem_get_capture_switch(mmixer->lines[x].elem, chn, &ival);
1026 if (i >= mctrld->u.cMultipleItems)
1028 TRACE("overflow\n");
1029 return MMSYSERR_INVALPARAM;
1031 TRACE("fVal[%i] = %sselected\n", i, (!ival ? "un" : ""));
1032 mcdb[i++].fValue = ival;
1038 FIXME("Unhandled controltype %s\n", getControlType(ct->c.dwControlType));
1039 return MMSYSERR_INVALPARAM;
1041 return MMSYSERR_NOERROR;
1043 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
1044 TRACE("MIXER_GETCONTROLDETAILSF_LISTTEXT (%d)\n", ctrl);
1046 if (ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MUX || ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MIXER)
1048 LPMIXERCONTROLDETAILS_LISTTEXTW mcdlt = mctrld->paDetails;
1051 for (i = j = 0; j < mmixer->chans; ++j)
1052 if (j != line && mmixer->lines[j].dst == line)
1054 if (i > mctrld->u.cMultipleItems)
1055 return MMSYSERR_INVALPARAM;
1056 mcdlt->dwParam1 = j;
1057 mcdlt->dwParam2 = mmixer->lines[j].component;
1058 lstrcpynW(mcdlt->szName, mmixer->lines[j].name, sizeof(mcdlt->szName) / sizeof(WCHAR));
1059 TRACE("Adding %i as %s\n", j, debugstr_w(mcdlt->szName));
1062 if (i < mctrld->u.cMultipleItems)
1063 return MMSYSERR_INVALPARAM;
1064 return MMSYSERR_NOERROR;
1066 FIXME ("Imagine this code being horribly broken and incomplete, introducing: reality\n");
1067 return MMSYSERR_INVALPARAM;
1070 WARN("Unknown flag (%08lx)\n", flags);
1071 return MMSYSERR_INVALPARAM;
1075 /* Set volume/capture channel/muted for control */
1076 static DWORD MIX_SetControlDetails(UINT wDevID, LPMIXERCONTROLDETAILS mctrld, DWORD_PTR flags)
1078 mixer *mmixer = MIX_GetMix(wDevID);
1079 DWORD ctrl, line, i;
1081 snd_mixer_elem_t * elem;
1084 return MMSYSERR_INVALPARAM;
1086 ctrl = mctrld->dwControlID;
1087 line = ctrl/CONTROLSPERLINE;
1089 if (mctrld->cbStruct != sizeof(*mctrld))
1091 WARN("Invalid size of mctrld %d\n", mctrld->cbStruct);
1092 return MMSYSERR_INVALPARAM;
1096 return MMSYSERR_BADDEVICEID;
1098 if (line >= mmixer->chans)
1100 WARN("Invalid line id: %d not in range of 0-%d\n", line, mmixer->chans-1);
1101 return MMSYSERR_INVALPARAM;
1104 if (!mmixer->controls[ctrl].enabled)
1106 WARN("Control %d not enabled\n", ctrl);
1107 return MIXERR_INVALCONTROL;
1110 ct = &mmixer->controls[ctrl];
1111 elem = mmixer->lines[line].elem;
1112 flags &= MIXER_SETCONTROLDETAILSF_QUERYMASK;
1115 case MIXER_SETCONTROLDETAILSF_VALUE:
1116 TRACE("MIXER_SETCONTROLDETAILSF_VALUE (%d)\n", ctrl);
1120 WARN("Unknown flag (%08lx)\n", flags);
1121 return MMSYSERR_INVALPARAM;
1124 switch (ct->c.dwControlType)
1126 case MIXERCONTROL_CONTROLTYPE_VOLUME:
1128 long min = 0, max = 0;
1130 LPMIXERCONTROLDETAILS_UNSIGNED mcdu;
1131 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
1133 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_UNSIGNED))
1135 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1136 return MMSYSERR_INVALPARAM;
1139 if (mctrld->cChannels != 1 && mmixer->lines[line].chans != mctrld->cChannels)
1141 WARN("Unsupported cChannels (%d instead of %d)\n", mctrld->cChannels, mmixer->lines[line].chans);
1142 return MMSYSERR_INVALPARAM;
1145 TRACE("%s MIXERCONTROLDETAILS_UNSIGNED[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1146 mcdu = mctrld->paDetails;
1148 for (chn=0; chn<mctrld->cChannels;++chn)
1150 TRACE("Chan %d value %d\n", chn, mcdu[chn].dwValue);
1153 /* There isn't always a capture volume, so in that case change playback volume */
1154 if (mmixer->lines[line].capt && snd_mixer_selem_has_capture_volume(elem))
1156 snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
1158 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1159 if (snd_mixer_selem_has_capture_channel(elem, chn))
1161 snd_mixer_selem_set_capture_volume(elem, chn, min + normalized(mcdu->dwValue, 65535, max));
1162 if (mctrld->cChannels != 1)
1168 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
1170 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1171 if (snd_mixer_selem_has_playback_channel(elem, chn))
1173 snd_mixer_selem_set_playback_volume(elem, chn, min + normalized(mcdu->dwValue, 65535, max));
1174 if (mctrld->cChannels != 1)
1181 case MIXERCONTROL_CONTROLTYPE_MUTE:
1182 case MIXERCONTROL_CONTROLTYPE_ONOFF:
1184 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1186 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1188 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1189 return MMSYSERR_INVALPARAM;
1192 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1194 mcdb = mctrld->paDetails;
1195 if (line == 1) /* Mute/unmute capturing */
1196 for (i = 0; i <= SND_MIXER_SCHN_LAST; ++i)
1198 if (snd_mixer_selem_has_capture_channel(elem, i))
1199 snd_mixer_selem_set_capture_switch(elem, i, !mcdb->fValue);
1202 for (i = 0; i <= SND_MIXER_SCHN_LAST; ++i)
1203 if (snd_mixer_selem_has_playback_channel(elem, i))
1204 snd_mixer_selem_set_playback_switch(elem, i, !mcdb->fValue);
1208 case MIXERCONTROL_CONTROLTYPE_MIXER:
1209 case MIXERCONTROL_CONTROLTYPE_MUX:
1211 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1213 int didone = 0, canone = (ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MUX);
1215 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1217 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1218 return MMSYSERR_INVALPARAM;
1221 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1222 mcdb = mctrld->paDetails;
1224 for (x=i=0; x < mmixer->chans; ++x)
1225 if (line != x && mmixer->lines[x].dst == line)
1227 TRACE("fVal[%i] (%s) = %i\n", i, debugstr_w(mmixer->lines[x].name), mcdb[i].fValue);
1228 if (i >= mctrld->u.cMultipleItems)
1230 TRACE("Too many items to fit, overflowing\n");
1231 return MIXERR_INVALVALUE;
1233 if (mcdb[i].fValue && canone && didone)
1235 TRACE("Nice try, but it's not going to work\n");
1236 elem_callback(mmixer->lines[1].elem, SND_CTL_EVENT_MASK_VALUE);
1237 return MIXERR_INVALVALUE;
1244 if (canone && !didone)
1246 TRACE("Nice try, this is not going to work either\n");
1247 elem_callback(mmixer->lines[1].elem, SND_CTL_EVENT_MASK_VALUE);
1248 return MIXERR_INVALVALUE;
1251 for (x = i = 0; x<mmixer->chans; ++x)
1252 if (line != x && mmixer->lines[x].dst == line)
1255 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1257 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1259 snd_mixer_selem_set_capture_switch(mmixer->lines[x].elem, chn, mcdb[i].fValue);
1264 /* If it's a MUX, it means that only 1 channel can be selected
1265 * and the other channels are unselected
1267 * For MIXER multiple sources are allowed, so unselect here
1272 for (x = i = 0; x<mmixer->chans; ++x)
1273 if (line != x && mmixer->lines[x].dst == line)
1275 if (!mcdb[i].fValue)
1276 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1278 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1280 snd_mixer_selem_set_capture_switch(mmixer->lines[x].elem, chn, mcdb[i].fValue);
1287 FIXME("Unhandled type %s\n", getControlType(ct->c.dwControlType));
1288 return MMSYSERR_INVALPARAM;
1290 return MMSYSERR_NOERROR;
1293 /* Here we give info over the source/dest line given by dwSource+dwDest or dwDest, respectively
1294 * It is also possible that a line is found by componenttype or target type, latter is not implemented yet
1295 * Most important values returned in struct:
1298 * line control count
1299 * amount of channels
1301 static DWORD MIX_GetLineInfo(UINT wDevID, LPMIXERLINEW Ml, DWORD_PTR flags)
1303 DWORD_PTR qf = flags & MIXER_GETLINEINFOF_QUERYMASK;
1304 mixer *mmixer = MIX_GetMix(wDevID);
1311 return MMSYSERR_INVALPARAM;
1316 WARN("Device %u not found\n", wDevID);
1317 return MMSYSERR_BADDEVICEID;
1320 if (Ml->cbStruct != sizeof(*Ml))
1322 WARN("invalid parameter: Ml->cbStruct = %d\n", Ml->cbStruct);
1323 return MMSYSERR_INVALPARAM;
1327 Ml->fdwLine = MIXERLINE_LINEF_DISCONNECTED;
1330 case MIXER_GETLINEINFOF_COMPONENTTYPE:
1332 Ml->dwLineID = 0xFFFF;
1333 TRACE("Looking for componenttype %d/%x\n", Ml->dwComponentType, Ml->dwComponentType);
1334 for (idx = 0; idx < mmixer->chans; ++idx)
1335 if (mmixer->lines[idx].component == Ml->dwComponentType)
1340 if (Ml->dwLineID == 0xFFFF)
1341 return MMSYSERR_KEYNOTFOUND;
1342 /* Now that we have lineid, fallback to lineid*/
1345 case MIXER_GETLINEINFOF_LINEID:
1346 if (Ml->dwLineID >= mmixer->chans)
1347 return MIXERR_INVALLINE;
1349 TRACE("MIXER_GETLINEINFOF_LINEID %d\n", Ml->dwLineID);
1350 Ml->dwDestination = mmixer->lines[Ml->dwLineID].dst;
1352 if (Ml->dwDestination != Ml->dwLineID)
1354 Ml->dwSource = getsrcfromline(mmixer, Ml->dwLineID);
1355 Ml->cConnections = 1;
1359 Ml->cConnections = getsrccntfromchan(mmixer, Ml->dwLineID);
1360 Ml->dwSource = 0xFFFFFFFF;
1362 TRACE("Connections %d, source %d\n", Ml->cConnections, Ml->dwSource);
1365 case MIXER_GETLINEINFOF_DESTINATION:
1366 if (Ml->dwDestination >= mmixer->dests)
1368 WARN("dest %d out of bounds\n", Ml->dwDestination);
1369 return MIXERR_INVALLINE;
1372 Ml->dwLineID = Ml->dwDestination;
1373 Ml->cConnections = getsrccntfromchan(mmixer, Ml->dwLineID);
1374 Ml->dwSource = 0xFFFFFFFF;
1377 case MIXER_GETLINEINFOF_SOURCE:
1378 if (Ml->dwDestination >= mmixer->dests)
1380 WARN("dest %d for source out of bounds\n", Ml->dwDestination);
1381 return MIXERR_INVALLINE;
1384 if (Ml->dwSource >= getsrccntfromchan(mmixer, Ml->dwDestination))
1386 WARN("src %d out of bounds\n", Ml->dwSource);
1387 return MIXERR_INVALLINE;
1390 Ml->dwLineID = getsrclinefromchan(mmixer, Ml->dwDestination, Ml->dwSource);
1391 Ml->cConnections = 1;
1394 case MIXER_GETLINEINFOF_TARGETTYPE:
1395 FIXME("TODO: TARGETTYPE, stub\n");
1396 return MMSYSERR_INVALPARAM;
1399 FIXME("Unknown query flag: %08lx\n", qf);
1400 return MMSYSERR_INVALPARAM;
1403 Ml->fdwLine &= ~MIXERLINE_LINEF_DISCONNECTED;
1404 Ml->fdwLine |= MIXERLINE_LINEF_ACTIVE;
1405 if (Ml->dwLineID >= mmixer->dests)
1406 Ml->fdwLine |= MIXERLINE_LINEF_SOURCE;
1408 mline = &mmixer->lines[Ml->dwLineID];
1409 Ml->dwComponentType = mline->component;
1410 Ml->cChannels = mmixer->lines[Ml->dwLineID].chans;
1413 for (i=CONTROLSPERLINE*Ml->dwLineID;i<CONTROLSPERLINE*(Ml->dwLineID+1); ++i)
1414 if (mmixer->controls[i].enabled)
1417 lstrcpynW(Ml->szShortName, mmixer->lines[Ml->dwLineID].name, sizeof(Ml->szShortName)/sizeof(WCHAR));
1418 lstrcpynW(Ml->szName, mmixer->lines[Ml->dwLineID].name, sizeof(Ml->szName)/sizeof(WCHAR));
1420 Ml->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
1422 Ml->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
1423 Ml->Target.dwDeviceID = 0xFFFFFFFF;
1424 Ml->Target.wMid = WINE_MIXER_MANUF_ID;
1425 Ml->Target.wPid = WINE_MIXER_PRODUCT_ID;
1426 Ml->Target.vDriverVersion = WINE_MIXER_VERSION;
1427 lstrcpynW(Ml->Target.szPname, mmixer->mixername, sizeof(Ml->Target.szPname)/sizeof(WCHAR));
1428 return MMSYSERR_NOERROR;
1431 /* Get the controls that belong to a certain line, either all or 1 */
1432 static DWORD MIX_GetLineControls(UINT wDevID, LPMIXERLINECONTROLSW mlc, DWORD_PTR flags)
1434 mixer *mmixer = MIX_GetMix(wDevID);
1438 if (!mlc || mlc->cbStruct != sizeof(*mlc))
1440 WARN("Invalid mlc %p, cbStruct: %d\n", mlc, (!mlc ? -1 : mlc->cbStruct));
1441 return MMSYSERR_INVALPARAM;
1444 if (mlc->cbmxctrl != sizeof(MIXERCONTROLW))
1446 WARN("cbmxctrl %d\n", mlc->cbmxctrl);
1447 return MMSYSERR_INVALPARAM;
1451 return MMSYSERR_BADDEVICEID;
1453 flags &= MIXER_GETLINECONTROLSF_QUERYMASK;
1455 if (flags == MIXER_GETLINECONTROLSF_ONEBYID)
1456 mlc->dwLineID = mlc->u.dwControlID / CONTROLSPERLINE;
1458 if (mlc->dwLineID >= mmixer->chans)
1460 TRACE("Invalid dwLineID %d\n", mlc->dwLineID);
1461 return MIXERR_INVALLINE;
1466 case MIXER_GETLINECONTROLSF_ALL:
1467 TRACE("line=%08x MIXER_GETLINECONTROLSF_ALL (%d)\n", mlc->dwLineID, mlc->cControls);
1468 for (i = 0; i < CONTROLSPERLINE; ++i)
1469 if (mmixer->controls[i+mlc->dwLineID * CONTROLSPERLINE].enabled)
1471 memcpy(&mlc->pamxctrl[j], &mmixer->controls[i+mlc->dwLineID * CONTROLSPERLINE].c, sizeof(MIXERCONTROLW));
1472 TRACE("Added %s (%s)\n", debugstr_w(mlc->pamxctrl[j].szShortName), debugstr_w(mlc->pamxctrl[j].szName));
1474 if (j > mlc->cControls)
1476 WARN("invalid parameter\n");
1477 return MMSYSERR_INVALPARAM;
1481 if (!j || mlc->cControls > j)
1483 WARN("invalid parameter\n");
1484 return MMSYSERR_INVALPARAM;
1487 case MIXER_GETLINECONTROLSF_ONEBYID:
1488 TRACE("line=%08x MIXER_GETLINECONTROLSF_ONEBYID (%x)\n", mlc->dwLineID, mlc->u.dwControlID);
1490 if (!mmixer->controls[mlc->u.dwControlID].enabled)
1491 return MIXERR_INVALCONTROL;
1493 mlc->pamxctrl[0] = mmixer->controls[mlc->u.dwControlID].c;
1495 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
1496 TRACE("line=%08x MIXER_GETLINECONTROLSF_ONEBYTYPE (%s)\n", mlc->dwLineID, getControlType(mlc->u.dwControlType));
1498 ct = mlc->u.dwControlType & MIXERCONTROL_CT_CLASS_MASK;
1499 for (i = 0; i <= CONTROLSPERLINE; ++i)
1501 const int ofs = i+mlc->dwLineID*CONTROLSPERLINE;
1502 if (i == CONTROLSPERLINE)
1504 WARN("invalid parameter: control %s not found\n", getControlType(mlc->u.dwControlType));
1505 return MIXERR_INVALCONTROL;
1507 if (mmixer->controls[ofs].enabled && (mmixer->controls[ofs].c.dwControlType & MIXERCONTROL_CT_CLASS_MASK) == ct)
1509 mlc->pamxctrl[0] = mmixer->controls[ofs].c;
1515 FIXME("Unknown flag %08lx\n", flags & MIXER_GETLINECONTROLSF_QUERYMASK);
1516 return MMSYSERR_INVALPARAM;
1519 return MMSYSERR_NOERROR;
1522 #endif /*HAVE_ALSA*/
1524 /**************************************************************************
1525 * mxdMessage (WINEALSA.3)
1527 DWORD WINAPI ALSA_mxdMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1528 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1532 TRACE("(%04X, %s, %08lX, %08lX, %08lX);\n", wDevID, getMessage(wMsg),
1533 dwUser, dwParam1, dwParam2);
1537 case DRVM_INIT: ALSA_MixerInit(); ret = MMSYSERR_NOERROR; break;
1538 case DRVM_EXIT: ALSA_MixerExit(); ret = MMSYSERR_NOERROR; break;
1539 /* All taken care of by driver initialisation */
1540 /* Unimplemented, and not needed */
1543 ret = MMSYSERR_NOERROR; break;
1546 ret = MIX_Open(wDevID, (LPMIXEROPENDESC) dwParam1, dwParam2); break;
1549 ret = MIX_Close(wDevID); break;
1551 case MXDM_GETDEVCAPS:
1552 ret = MIX_GetDevCaps(wDevID, (LPMIXERCAPS2W)dwParam1, dwParam2); break;
1554 case MXDM_GETLINEINFO:
1555 ret = MIX_GetLineInfo(wDevID, (LPMIXERLINEW)dwParam1, dwParam2); break;
1557 case MXDM_GETLINECONTROLS:
1558 ret = MIX_GetLineControls(wDevID, (LPMIXERLINECONTROLSW)dwParam1, dwParam2); break;
1560 case MXDM_GETCONTROLDETAILS:
1561 ret = MIX_GetControlDetails(wDevID, (LPMIXERCONTROLDETAILS)dwParam1, dwParam2); break;
1563 case MXDM_SETCONTROLDETAILS:
1564 ret = MIX_SetControlDetails(wDevID, (LPMIXERCONTROLDETAILS)dwParam1, dwParam2); break;
1566 case MXDM_GETNUMDEVS:
1570 WARN("unknown message %s!\n", getMessage(wMsg));
1571 return MMSYSERR_NOTSUPPORTED;
1574 TRACE("Returning %08X\n", ret);
1577 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1579 return MMSYSERR_NOTENABLED;
1580 #endif /*HAVE_ALSA*/