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)
80 #define MSG_TO_STR(x) case x: return #x;
82 MSG_TO_STR(DRVM_INIT);
83 MSG_TO_STR(DRVM_EXIT);
84 MSG_TO_STR(DRVM_ENABLE);
85 MSG_TO_STR(DRVM_DISABLE);
86 MSG_TO_STR(MXDM_GETDEVCAPS);
87 MSG_TO_STR(MXDM_GETLINEINFO);
88 MSG_TO_STR(MXDM_GETNUMDEVS);
89 MSG_TO_STR(MXDM_OPEN);
90 MSG_TO_STR(MXDM_CLOSE);
91 MSG_TO_STR(MXDM_GETLINECONTROLS);
92 MSG_TO_STR(MXDM_GETCONTROLDETAILS);
93 MSG_TO_STR(MXDM_SETCONTROLDETAILS);
97 return wine_dbg_sprintf("UNKNOWN(%08x)", uMsg);
100 static const char * getControlType(DWORD dwControlType)
102 #define TYPE_TO_STR(x) case x: return #x;
103 switch (dwControlType) {
104 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_CUSTOM);
105 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BOOLEANMETER);
106 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SIGNEDMETER);
107 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PEAKMETER);
108 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_UNSIGNEDMETER);
109 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BOOLEAN);
110 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_ONOFF);
111 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MUTE);
112 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MONO);
113 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_LOUDNESS);
114 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_STEREOENH);
115 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BASS_BOOST);
116 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BUTTON);
117 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_DECIBELS);
118 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SIGNED);
119 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_UNSIGNED);
120 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PERCENT);
121 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SLIDER);
122 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PAN);
123 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_QSOUNDPAN);
124 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_FADER);
125 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_VOLUME);
126 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BASS);
127 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_TREBLE);
128 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_EQUALIZER);
129 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SINGLESELECT);
130 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MUX);
131 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MULTIPLESELECT);
132 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MIXER);
133 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MICROTIME);
134 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MILLITIME);
137 return wine_dbg_sprintf("UNKNOWN(%08x)", dwControlType);
140 /* A simple declaration of a line control
141 * These are each of the channels that show up
143 typedef struct line {
144 /* Name we present to outside world */
145 WCHAR name[MAXPNAMELEN];
151 snd_mixer_elem_t *elem;
154 /* A control structure, with toggle enabled switch
155 * Control structures control volume, muted, which capture source
157 typedef struct control {
166 WCHAR mixername[MAXPNAMELEN];
169 LPDRVCALLBACK callback;
170 DWORD_PTR callbackpriv;
177 #define MAX_MIXERS 32
178 #define CONTROLSPERLINE 3
182 static int cards = 0;
183 static mixer mixdev[MAX_MIXERS];
184 static HANDLE thread;
185 static int elem_callback(snd_mixer_elem_t *elem, unsigned int mask);
186 static DWORD WINAPI ALSA_MixerPollThread(LPVOID lParam);
187 static CRITICAL_SECTION elem_crst;
188 static int msg_pipe[2];
191 /* found channel names in alsa lib, alsa api doesn't have another way for this
192 * map name -> componenttype, worst case we get a wrong componenttype which is
196 static const struct mixerlinetype {
197 const char *name; DWORD cmpt;
199 { "Master", MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, },
200 { "Capture", MIXERLINE_COMPONENTTYPE_DST_WAVEIN, },
201 { "PCM", MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT, },
202 { "PC Speaker", MIXERLINE_COMPONENTTYPE_SRC_PCSPEAKER, },
203 { "Synth", MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER, },
204 { "Headphone", MIXERLINE_COMPONENTTYPE_DST_HEADPHONES, },
205 { "Mic", MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, },
206 { "Aux", MIXERLINE_COMPONENTTYPE_SRC_UNDEFINED, },
207 { "CD", MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC, },
208 { "Line", MIXERLINE_COMPONENTTYPE_SRC_LINE, },
209 { "Phone", MIXERLINE_COMPONENTTYPE_SRC_TELEPHONE, },
210 { "Digital", MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, },
211 { "Front Mic", MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, },
214 /* Map name to MIXERLINE_COMPONENTTYPE_XXX */
215 static int getcomponenttype(const char *name)
218 for (x=0; x< sizeof(converttable)/sizeof(converttable[0]); ++x)
219 if (!strcasecmp(name, converttable[x].name))
221 TRACE("%d -> %s\n", x, name);
222 return converttable[x].cmpt;
224 WARN("Unknown mixer name %s, probably harmless\n", name);
225 return MIXERLINE_COMPONENTTYPE_SRC_UNDEFINED;
228 /* Is this control suited for showing up? */
229 static int blacklisted(snd_mixer_elem_t *elem)
231 const char *name = snd_mixer_selem_get_name(elem);
234 if (!snd_mixer_selem_has_playback_volume(elem) &&
235 !snd_mixer_selem_has_capture_volume(elem))
238 TRACE("%s: %x\n", name, blisted);
242 static void fillcontrols(mixer *mmixer)
245 for (id = 0; id < mmixer->chans; ++id)
247 line *mline = &mmixer->lines[id];
248 int ofs = CONTROLSPERLINE * id;
252 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;
339 MultiByteToWideChar(CP_UNIXCP, 0, snd_mixer_selem_get_name(mastelem), -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
340 mline->component = getcomponenttype(snd_mixer_selem_get_name(mastelem));
343 mline->elem = mastelem;
344 mline->chans = chans(mmixer, mastelem, 0);
346 snd_mixer_elem_set_callback(mastelem, &elem_callback);
347 snd_mixer_elem_set_callback_private(mastelem, mmixer);
349 MultiByteToWideChar(CP_UNIXCP, 0, "Empty Master Element", -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
353 * Note: since mmixer->dests = 1, it means only playback control is visible
354 * This makes sense, because if there are no capture sources capture control
355 * can't do anything and should be invisible */
357 /* Control 1 is reserved for capture even when not enabled */
361 MultiByteToWideChar(CP_UNIXCP, 0, snd_mixer_selem_get_name(captelem), -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
362 mline->component = getcomponenttype(snd_mixer_selem_get_name(captelem));
365 mline->elem = captelem;
366 mline->chans = chans(mmixer, captelem, 1);
368 snd_mixer_elem_set_callback(captelem, &elem_callback);
369 snd_mixer_elem_set_callback_private(captelem, mmixer);
372 for (elem = snd_mixer_first_elem(mmixer->mix); elem; elem = snd_mixer_elem_next(elem))
373 if (elem != mastelem && elem != captelem && !blacklisted(elem))
375 const char * name = snd_mixer_selem_get_name(elem);
376 DWORD comp = getcomponenttype(name);
378 if (snd_mixer_selem_has_playback_volume(elem) &&
379 (snd_mixer_selem_has_capture_volume(elem) || comp != MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE))
381 (++mline)->component = comp;
382 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, mline->name, MAXPNAMELEN);
383 mline->capt = mline->dst = 0;
385 mline->chans = chans(mmixer, elem, 0);
390 if (capt && (snd_mixer_selem_has_capture_volume(elem) || comp == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE))
392 (++mline)->component = comp;
393 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, mline->name, MAXPNAMELEN);
394 mline->capt = mline->dst = 1;
396 mline->chans = chans(mmixer, elem, 1);
399 snd_mixer_elem_set_callback(elem, &elem_callback);
400 snd_mixer_elem_set_callback_private(elem, mmixer);
404 static void filllines_no_master(mixer *mmixer, snd_mixer_elem_t *captelem, int capt)
406 line *mline = mmixer->lines;
408 MultiByteToWideChar(CP_UNIXCP, 0, snd_mixer_selem_get_name(captelem), -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
409 mline->component = getcomponenttype(snd_mixer_selem_get_name(captelem));
412 mline->elem = captelem;
413 mline->chans = chans(mmixer, captelem, 1);
415 snd_mixer_elem_set_callback(captelem, &elem_callback);
416 snd_mixer_elem_set_callback_private(captelem, mmixer);
419 /* Windows api wants to have a 'master' device to which all slaves are attached
420 * There are 2 ones in this code:
421 * - 'Master', fall back to 'Headphone' if unavailable, and if that's not available 'PCM'
423 * Capture might not always be available, so should be prepared to be without if needed
426 static void ALSA_MixerInit(void)
429 snd_ctl_card_info_t *info;
431 info = HeapAlloc( GetProcessHeap(), 0, snd_ctl_card_info_sizeof());
432 for (x = 0; x < MAX_MIXERS; ++x)
434 int card, err, capcontrols = 0, total_elems = 0;
435 char cardind[6], cardname[10];
438 snd_mixer_elem_t *elem, *mastelem = NULL, *headelem = NULL, *captelem = NULL, *pcmelem = NULL, *lineelem = NULL, *micelem = NULL;
440 memset(info, 0, snd_ctl_card_info_sizeof());
441 memset(&mixdev[mixnum], 0, sizeof(*mixdev));
442 snprintf(cardind, sizeof(cardind), "%d", x);
443 card = snd_card_get_index(cardind);
447 snprintf(cardname, sizeof(cardname), "hw:%d", card);
449 err = snd_ctl_open(&ctl, cardname, 0);
452 WARN("Cannot open card: %s\n", snd_strerror(err));
456 err = snd_ctl_card_info(ctl, info);
459 WARN("Cannot get card info: %s\n", snd_strerror(err));
464 MultiByteToWideChar(CP_UNIXCP, 0, snd_ctl_card_info_get_name(info), -1, mixdev[mixnum].mixername, sizeof(mixdev[mixnum].mixername)/sizeof(WCHAR));
467 err = snd_mixer_open(&mixdev[mixnum].mix, 0);
470 WARN("Error occurred opening mixer: %s\n", snd_strerror(err));
474 err = snd_mixer_attach(mixdev[mixnum].mix, cardname);
478 err = snd_mixer_selem_register(mixdev[mixnum].mix, NULL, NULL);
482 err = snd_mixer_load(mixdev[mixnum].mix);
486 /* First, lets see what's available..
487 * If there are multiple Master or Captures, all except 1 will be added as slaves
489 total_elems = snd_mixer_get_count(mixdev[mixnum].mix);
490 TRACE("Total elems: %d\n", total_elems);
492 for (elem = snd_mixer_first_elem(mixdev[mixnum].mix); elem; elem = snd_mixer_elem_next(elem))
493 if (!strcasecmp(snd_mixer_selem_get_name(elem), "Master") && !mastelem)
496 ++(mixdev[mixnum].chans);
498 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "Capture") && !captelem)
500 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "Mic") && !micelem && !mastelem && total_elems == 1)
501 /* this is what snd-usb-audio mics look like; just a Mic control and that's it.*/
503 else if (!blacklisted(elem))
505 DWORD comp = getcomponenttype(snd_mixer_selem_get_name(elem));
508 /* Work around buggy drivers: Make this a capture control if the name is recognised as a microphone */
509 if (snd_mixer_selem_has_capture_volume(elem))
511 else if (comp == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE)
517 if (!skip && snd_mixer_selem_has_playback_volume(elem))
519 if (!strcasecmp(snd_mixer_selem_get_name(elem), "Headphone") && !headelem)
521 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "PCM") && !pcmelem)
523 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "Line") && !lineelem)
525 ++(mixdev[mixnum].chans);
529 /* Add dummy capture channel, wanted by Windows */
530 mixdev[mixnum].chans += 1;
532 /* If there is only 'Capture' and 'Master', this device is not worth it */
533 if (mixdev[mixnum].chans == 2)
535 WARN("No channels found, skipping device!\n");
539 /* Master element can't have a capture control in this code, so
540 * if Headphone or PCM is promoted to master, unset its capture control */
541 if (headelem && !mastelem)
543 /* Using 'Headphone' as master device */
545 capcontrols -= !!snd_mixer_selem_has_capture_switch(mastelem);
547 else if (pcmelem && !mastelem)
549 /* Use 'PCM' as master device */
551 capcontrols -= !!snd_mixer_selem_has_capture_switch(mastelem);
553 else if (lineelem && !mastelem)
555 /* Use 'Line' as master device */
557 capcontrols -= !!snd_mixer_selem_has_capture_switch(mastelem);
559 else if (!mastelem && !captelem && !micelem)
561 /* If there is nothing sensible that can act as 'Master' control, something is wrong */
562 FIXME("No master control found on %s, disabling mixer\n", snd_ctl_card_info_get_name(info));
566 if (!captelem || !capcontrols)
568 /* Can't enable capture, so disabling it
569 * Note: capture control will still exist because
570 * dwLineID 0 and 1 are reserved for Master and Capture
572 WARN("No use enabling capture part of mixer, capture control found: %s, amount of capture controls: %d\n",
573 (!captelem ? "no" : "yes"), capcontrols);
575 mixdev[mixnum].dests = 1;
579 mixdev[mixnum].chans += capcontrols;
580 mixdev[mixnum].dests = 2;
583 mixdev[mixnum].lines = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(line) * mixdev[mixnum].chans);
584 mixdev[mixnum].controls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(control) * CONTROLSPERLINE*mixdev[mixnum].chans);
586 if (!mixdev[mixnum].lines || !mixdev[mixnum].controls)
590 filllines(&mixdev[mixnum], mastelem, captelem, capcontrols);
592 filllines_no_master(&mixdev[mixnum], micelem, 1);
593 fillcontrols(&mixdev[mixnum]);
595 TRACE("%s: Amount of controls: %i/%i, name: %s\n", cardname, mixdev[mixnum].dests, mixdev[mixnum].chans, debugstr_w(mixdev[mixnum].mixername));
600 WARN("Error occurred initialising mixer: %s\n", snd_strerror(err));
602 HeapFree(GetProcessHeap(), 0, mixdev[mixnum].lines);
603 HeapFree(GetProcessHeap(), 0, mixdev[mixnum].controls);
604 snd_mixer_close(mixdev[mixnum].mix);
607 HeapFree( GetProcessHeap(), 0, info );
609 /* There is no trouble with already assigning callbacks without initialising critsect:
610 * Callbacks only occur when snd_mixer_handle_events is called (only happens in thread)
612 InitializeCriticalSection(&elem_crst);
613 elem_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_MIXER.elem_crst");
617 static void ALSA_MixerExit(void)
623 WARN("Callback thread still alive, terminating uncleanly, refcnt: %d\n", refcnt);
624 /* Least we can do is making sure we're not in 'foreign' code */
625 EnterCriticalSection(&elem_crst);
626 TerminateThread(thread, 1);
628 LeaveCriticalSection(&elem_crst);
631 TRACE("Cleaning up\n");
633 elem_crst.DebugInfo->Spare[0] = 0;
634 DeleteCriticalSection(&elem_crst);
635 for (x = 0; x < cards; ++x)
637 snd_mixer_close(mixdev[x].mix);
638 HeapFree(GetProcessHeap(), 0, mixdev[x].lines);
639 HeapFree(GetProcessHeap(), 0, mixdev[x].controls);
644 static mixer* MIX_GetMix(UINT wDevID)
650 WARN("Invalid mixer id: %d\n", wDevID);
654 mmixer = &mixdev[wDevID];
658 /* Since alsa doesn't tell what exactly changed, just assume all affected controls changed */
659 static int elem_callback(snd_mixer_elem_t *elem, unsigned int type)
661 mixer *mmixer = snd_mixer_elem_get_callback_private(elem);
663 BOOL captchanged = 0;
665 if (type != SND_CTL_EVENT_MASK_VALUE)
670 EnterCriticalSection(&elem_crst);
672 if (!mmixer->callback)
675 for (x=0; x<mmixer->chans; ++x)
677 const int ofs = CONTROLSPERLINE*x;
678 if (elem != mmixer->lines[x].elem)
681 if (mmixer->lines[x].capt)
684 TRACE("Found changed control %s\n", debugstr_w(mmixer->lines[x].name));
685 mmixer->callback(mmixer->hmx, MM_MIXM_LINE_CHANGE, mmixer->callbackpriv, x, 0);
686 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, ofs, 0);
688 if (mmixer->controls[ofs+OFS_MUTE].enabled)
689 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, ofs+OFS_MUTE, 0);
692 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, CONTROLSPERLINE+OFS_MUX, 0);
695 LeaveCriticalSection(&elem_crst);
700 static DWORD WINAPI ALSA_MixerPollThread(LPVOID lParam)
702 struct pollfd *pfds = NULL;
703 int x, y, err, mcnt, count = 1;
705 TRACE("%p\n", lParam);
707 for (x = 0; x < cards; ++x)
708 count += snd_mixer_poll_descriptors_count(mixdev[x].mix);
710 TRACE("Counted %d descriptors\n", count);
711 pfds = HeapAlloc(GetProcessHeap(), 0, count * sizeof(struct pollfd));
715 WARN("Out of memory\n");
719 pfds[0].fd = msg_pipe[0];
720 pfds[0].events = POLLIN;
723 for (x = 0; x < cards; ++x)
724 y += snd_mixer_poll_descriptors(mixdev[x].mix, &pfds[y], count - y);
726 while ((err = poll(pfds, (unsigned int) count, -1)) >= 0 || errno == EINTR || errno == EAGAIN)
728 if (pfds[0].revents & POLLIN)
732 for (x = y = 0; x < cards; ++x)
734 int j, max = snd_mixer_poll_descriptors_count(mixdev[x].mix);
735 for (j = 0; j < max; ++j)
736 if (pfds[mcnt+j].revents)
738 y += snd_mixer_handle_events(mixdev[x].mix);
744 TRACE("Handled %d events\n", y);
748 TRACE("Shutting down\n");
749 HeapFree(GetProcessHeap(), 0, pfds);
751 y = read(msg_pipe[0], &x, sizeof(x));
757 static DWORD MIX_Open(UINT wDevID, LPMIXEROPENDESC desc, DWORD_PTR flags)
759 mixer *mmixer = MIX_GetMix(wDevID);
761 return MMSYSERR_BADDEVICEID;
763 flags &= CALLBACK_TYPEMASK;
769 case CALLBACK_FUNCTION:
773 FIXME("Unhandled callback type: %08lx\n", flags & CALLBACK_TYPEMASK);
774 return MIXERR_INVALVALUE;
777 mmixer->callback = (LPDRVCALLBACK)desc->dwCallback;
778 mmixer->callbackpriv = desc->dwInstance;
779 mmixer->hmx = (HDRVR)desc->hmx;
782 if (InterlockedIncrement(&refcnt) == 1)
784 if (pipe(msg_pipe) >= 0)
786 thread = CreateThread(NULL, 0, ALSA_MixerPollThread, NULL, 0, NULL);
791 msg_pipe[0] = msg_pipe[1] = -1;
795 msg_pipe[0] = msg_pipe[1] = -1;
798 return MMSYSERR_NOERROR;
801 static DWORD MIX_Close(UINT wDevID)
804 mixer *mmixer = MIX_GetMix(wDevID);
806 return MMSYSERR_BADDEVICEID;
808 EnterCriticalSection(&elem_crst);
809 mmixer->callback = 0;
810 LeaveCriticalSection(&elem_crst);
812 if (!InterlockedDecrement(&refcnt))
814 if (write(msg_pipe[1], &x, sizeof(x)) > 0)
816 TRACE("Shutting down thread...\n");
817 WaitForSingleObject(thread, INFINITE);
822 return MMSYSERR_NOERROR;
825 static DWORD MIX_GetDevCaps(UINT wDevID, LPMIXERCAPS2W caps, DWORD_PTR parm2)
827 mixer *mmixer = MIX_GetMix(wDevID);
831 return MMSYSERR_INVALPARAM;
834 return MMSYSERR_BADDEVICEID;
836 memset(&capsW, 0, sizeof(MIXERCAPS2W));
838 capsW.wMid = WINE_MIXER_MANUF_ID;
839 capsW.wPid = WINE_MIXER_PRODUCT_ID;
840 capsW.vDriverVersion = WINE_MIXER_VERSION;
842 lstrcpynW(capsW.szPname, mmixer->mixername, sizeof(capsW.szPname)/sizeof(WCHAR));
843 capsW.cDestinations = mmixer->dests;
844 memcpy(caps, &capsW, min(parm2, sizeof(capsW)));
845 return MMSYSERR_NOERROR;
848 /* convert win32 volume to alsa volume, and vice versa */
849 static INT normalized(INT value, INT prevmax, INT nextmax)
851 int ret = MulDiv(value, nextmax, prevmax);
853 /* Have to stay in range */
854 TRACE("%d/%d -> %d/%d\n", value, prevmax, ret, nextmax);
863 /* get amount of sources for dest */
864 static int getsrccntfromchan(mixer *mmixer, int dad)
868 for (i=0; i<mmixer->chans; ++i)
869 if (i != dad && mmixer->lines[i].dst == dad)
874 FIXME("No src found for %i (%s)?\n", dad, debugstr_w(mmixer->lines[dad].name));
878 /* find lineid for source 'num' with dest 'dad' */
879 static int getsrclinefromchan(mixer *mmixer, int dad, int num)
882 for (i=0; i<mmixer->chans; ++i)
883 if (i != dad && mmixer->lines[i].dst == dad)
889 WARN("No src found for src %i from dest %i\n", num, dad);
893 /* get the source number belonging to line */
894 static int getsrcfromline(mixer *mmixer, int line)
896 int i, j=0, dad = mmixer->lines[line].dst;
898 for (i=0; i<mmixer->chans; ++i)
899 if (i != dad && mmixer->lines[i].dst == dad)
905 WARN("No src found for line %i with dad %i\n", line, dad);
909 /* Get volume/muted/capture channel */
910 static DWORD MIX_GetControlDetails(UINT wDevID, LPMIXERCONTROLDETAILS mctrld, DWORD_PTR flags)
912 mixer *mmixer = MIX_GetMix(wDevID);
918 return MMSYSERR_INVALPARAM;
920 ctrl = mctrld->dwControlID;
921 line = ctrl/CONTROLSPERLINE;
923 if (mctrld->cbStruct != sizeof(*mctrld))
924 return MMSYSERR_INVALPARAM;
927 return MMSYSERR_BADDEVICEID;
929 if (line >= mmixer->chans || !mmixer->controls[ctrl].enabled)
930 return MIXERR_INVALCONTROL;
932 ct = &mmixer->controls[ctrl];
934 flags &= MIXER_GETCONTROLDETAILSF_QUERYMASK;
937 case MIXER_GETCONTROLDETAILSF_VALUE:
938 TRACE("MIXER_GETCONTROLDETAILSF_VALUE (%d/%d)\n", ctrl, line);
939 switch (ct->c.dwControlType)
941 case MIXERCONTROL_CONTROLTYPE_VOLUME:
943 long min = 0, max = 0, vol = 0;
945 LPMIXERCONTROLDETAILS_UNSIGNED mcdu;
946 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
948 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_UNSIGNED))
950 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
951 return MMSYSERR_INVALPARAM;
954 TRACE("%s MIXERCONTROLDETAILS_UNSIGNED[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
956 mcdu = mctrld->paDetails;
958 if (mctrld->cChannels != 1 && mmixer->lines[line].chans != mctrld->cChannels)
960 WARN("Unsupported cChannels (%d instead of %d)\n", mctrld->cChannels, mmixer->lines[line].chans);
961 return MMSYSERR_INVALPARAM;
964 if (mmixer->lines[line].capt && snd_mixer_selem_has_capture_volume(elem)) {
965 snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
966 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
967 if (snd_mixer_selem_has_capture_channel(elem, chn))
969 snd_mixer_selem_get_capture_volume(elem, chn, &vol);
970 mcdu->dwValue = normalized(vol - min, max, 65535);
971 if (mctrld->cChannels == 1)
976 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
978 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
979 if (snd_mixer_selem_has_playback_channel(elem, chn))
981 snd_mixer_selem_get_playback_volume(elem, chn, &vol);
982 mcdu->dwValue = normalized(vol - min, max, 65535);
983 if (mctrld->cChannels == 1)
989 return MMSYSERR_NOERROR;
992 case MIXERCONTROL_CONTROLTYPE_ONOFF:
993 case MIXERCONTROL_CONTROLTYPE_MUTE:
995 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
997 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
999 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1001 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1002 return MMSYSERR_INVALPARAM;
1005 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1007 mcdb = mctrld->paDetails;
1010 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1012 if (!snd_mixer_selem_has_capture_channel(elem, chn))
1014 snd_mixer_selem_get_capture_switch(elem, chn, &ival);
1018 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1020 if (!snd_mixer_selem_has_playback_channel(elem, chn))
1022 snd_mixer_selem_get_playback_switch(elem, chn, &ival);
1026 if (chn > SND_MIXER_SCHN_LAST)
1028 TRACE("can't find active channel\n");
1029 return MMSYSERR_INVALPARAM; /* fixme: what's right error? */
1032 mcdb->fValue = !ival;
1033 TRACE("=> %s\n", mcdb->fValue ? "on" : "off");
1034 return MMSYSERR_NOERROR;
1036 case MIXERCONTROL_CONTROLTYPE_MIXER:
1037 case MIXERCONTROL_CONTROLTYPE_MUX:
1039 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1040 int x, i=0, ival = 0, chn;
1042 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1044 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1045 return MMSYSERR_INVALPARAM;
1048 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1050 mcdb = mctrld->paDetails;
1052 for (x = 0; x<mmixer->chans; ++x)
1053 if (line != x && mmixer->lines[x].dst == line)
1056 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1058 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1060 snd_mixer_selem_get_capture_switch(mmixer->lines[x].elem, chn, &ival);
1064 if (i >= mctrld->u.cMultipleItems)
1066 TRACE("overflow\n");
1067 return MMSYSERR_INVALPARAM;
1069 TRACE("fVal[%i] = %sselected\n", i, (!ival ? "un" : ""));
1070 mcdb[i++].fValue = ival;
1076 FIXME("Unhandled controltype %s\n", getControlType(ct->c.dwControlType));
1077 return MMSYSERR_INVALPARAM;
1079 return MMSYSERR_NOERROR;
1081 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
1082 TRACE("MIXER_GETCONTROLDETAILSF_LISTTEXT (%d)\n", ctrl);
1084 if (ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MUX || ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MIXER)
1086 LPMIXERCONTROLDETAILS_LISTTEXTW mcdlt = mctrld->paDetails;
1089 for (i = j = 0; j < mmixer->chans; ++j)
1090 if (j != line && mmixer->lines[j].dst == line)
1092 if (i > mctrld->u.cMultipleItems)
1093 return MMSYSERR_INVALPARAM;
1094 mcdlt->dwParam1 = j;
1095 mcdlt->dwParam2 = mmixer->lines[j].component;
1096 lstrcpynW(mcdlt->szName, mmixer->lines[j].name, sizeof(mcdlt->szName) / sizeof(WCHAR));
1097 TRACE("Adding %i as %s\n", j, debugstr_w(mcdlt->szName));
1100 if (i < mctrld->u.cMultipleItems)
1101 return MMSYSERR_INVALPARAM;
1102 return MMSYSERR_NOERROR;
1104 FIXME ("Imagine this code being horribly broken and incomplete, introducing: reality\n");
1105 return MMSYSERR_INVALPARAM;
1108 WARN("Unknown flag (%08lx)\n", flags);
1109 return MMSYSERR_INVALPARAM;
1113 /* Set volume/capture channel/muted for control */
1114 static DWORD MIX_SetControlDetails(UINT wDevID, LPMIXERCONTROLDETAILS mctrld, DWORD_PTR flags)
1116 mixer *mmixer = MIX_GetMix(wDevID);
1117 DWORD ctrl, line, i;
1119 snd_mixer_elem_t * elem;
1122 return MMSYSERR_INVALPARAM;
1124 ctrl = mctrld->dwControlID;
1125 line = ctrl/CONTROLSPERLINE;
1127 if (mctrld->cbStruct != sizeof(*mctrld))
1129 WARN("Invalid size of mctrld %d\n", mctrld->cbStruct);
1130 return MMSYSERR_INVALPARAM;
1134 return MMSYSERR_BADDEVICEID;
1136 if (line >= mmixer->chans)
1138 WARN("Invalid line id: %d not in range of 0-%d\n", line, mmixer->chans-1);
1139 return MMSYSERR_INVALPARAM;
1142 if (!mmixer->controls[ctrl].enabled)
1144 WARN("Control %d not enabled\n", ctrl);
1145 return MIXERR_INVALCONTROL;
1148 ct = &mmixer->controls[ctrl];
1149 elem = mmixer->lines[line].elem;
1150 flags &= MIXER_SETCONTROLDETAILSF_QUERYMASK;
1153 case MIXER_SETCONTROLDETAILSF_VALUE:
1154 TRACE("MIXER_SETCONTROLDETAILSF_VALUE (%d)\n", ctrl);
1158 WARN("Unknown flag (%08lx)\n", flags);
1159 return MMSYSERR_INVALPARAM;
1162 switch (ct->c.dwControlType)
1164 case MIXERCONTROL_CONTROLTYPE_VOLUME:
1166 long min = 0, max = 0;
1168 LPMIXERCONTROLDETAILS_UNSIGNED mcdu;
1169 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
1171 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_UNSIGNED))
1173 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1174 return MMSYSERR_INVALPARAM;
1177 if (mctrld->cChannels != 1 && mmixer->lines[line].chans != mctrld->cChannels)
1179 WARN("Unsupported cChannels (%d instead of %d)\n", mctrld->cChannels, mmixer->lines[line].chans);
1180 return MMSYSERR_INVALPARAM;
1183 TRACE("%s MIXERCONTROLDETAILS_UNSIGNED[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1184 mcdu = mctrld->paDetails;
1186 for (chn=0; chn<mctrld->cChannels;++chn)
1188 TRACE("Chan %d value %d\n", chn, mcdu[chn].dwValue);
1191 /* There isn't always a capture volume, so in that case change playback volume */
1192 if (mmixer->lines[line].capt && snd_mixer_selem_has_capture_volume(elem))
1194 snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
1196 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1197 if (snd_mixer_selem_has_capture_channel(elem, chn))
1199 snd_mixer_selem_set_capture_volume(elem, chn, min + normalized(mcdu->dwValue, 65535, max));
1200 if (mctrld->cChannels != 1)
1206 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
1208 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1209 if (snd_mixer_selem_has_playback_channel(elem, chn))
1211 snd_mixer_selem_set_playback_volume(elem, chn, min + normalized(mcdu->dwValue, 65535, max));
1212 if (mctrld->cChannels != 1)
1219 case MIXERCONTROL_CONTROLTYPE_MUTE:
1220 case MIXERCONTROL_CONTROLTYPE_ONOFF:
1222 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1224 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1226 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1227 return MMSYSERR_INVALPARAM;
1230 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1232 mcdb = mctrld->paDetails;
1233 if (line == 1) /* Mute/unmute capturing */
1234 for (i = 0; i <= SND_MIXER_SCHN_LAST; ++i)
1236 if (snd_mixer_selem_has_capture_channel(elem, i))
1237 snd_mixer_selem_set_capture_switch(elem, i, !mcdb->fValue);
1240 for (i = 0; i <= SND_MIXER_SCHN_LAST; ++i)
1241 if (snd_mixer_selem_has_playback_channel(elem, i))
1242 snd_mixer_selem_set_playback_switch(elem, i, !mcdb->fValue);
1246 case MIXERCONTROL_CONTROLTYPE_MIXER:
1247 case MIXERCONTROL_CONTROLTYPE_MUX:
1249 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1251 int didone = 0, canone = (ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MUX);
1253 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1255 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1256 return MMSYSERR_INVALPARAM;
1259 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1260 mcdb = mctrld->paDetails;
1262 for (x=i=0; x < mmixer->chans; ++x)
1263 if (line != x && mmixer->lines[x].dst == line)
1265 TRACE("fVal[%i] (%s) = %i\n", i, debugstr_w(mmixer->lines[x].name), mcdb[i].fValue);
1266 if (i >= mctrld->u.cMultipleItems)
1268 TRACE("Too many items to fit, overflowing\n");
1269 return MIXERR_INVALVALUE;
1271 if (mcdb[i].fValue && canone && didone)
1273 TRACE("Nice try, but it's not going to work\n");
1274 elem_callback(mmixer->lines[1].elem, SND_CTL_EVENT_MASK_VALUE);
1275 return MIXERR_INVALVALUE;
1282 if (canone && !didone)
1284 TRACE("Nice try, this is not going to work either\n");
1285 elem_callback(mmixer->lines[1].elem, SND_CTL_EVENT_MASK_VALUE);
1286 return MIXERR_INVALVALUE;
1289 for (x = i = 0; x<mmixer->chans; ++x)
1290 if (line != x && mmixer->lines[x].dst == line)
1293 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1295 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1297 snd_mixer_selem_set_capture_switch(mmixer->lines[x].elem, chn, mcdb[i].fValue);
1302 /* If it's a MUX, it means that only 1 channel can be selected
1303 * and the other channels are unselected
1305 * For MIXER multiple sources are allowed, so unselect here
1310 for (x = i = 0; x<mmixer->chans; ++x)
1311 if (line != x && mmixer->lines[x].dst == line)
1313 if (!mcdb[i].fValue)
1314 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1316 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1318 snd_mixer_selem_set_capture_switch(mmixer->lines[x].elem, chn, mcdb[i].fValue);
1325 FIXME("Unhandled type %s\n", getControlType(ct->c.dwControlType));
1326 return MMSYSERR_INVALPARAM;
1328 return MMSYSERR_NOERROR;
1331 /* Here we give info over the source/dest line given by dwSource+dwDest or dwDest, respectively
1332 * It is also possible that a line is found by componenttype or target type, latter is not implemented yet
1333 * Most important values returned in struct:
1336 * line control count
1337 * amount of channels
1339 static DWORD MIX_GetLineInfo(UINT wDevID, LPMIXERLINEW Ml, DWORD_PTR flags)
1341 DWORD_PTR qf = flags & MIXER_GETLINEINFOF_QUERYMASK;
1342 mixer *mmixer = MIX_GetMix(wDevID);
1349 return MMSYSERR_INVALPARAM;
1354 WARN("Device %u not found\n", wDevID);
1355 return MMSYSERR_BADDEVICEID;
1358 if (Ml->cbStruct != sizeof(*Ml))
1360 WARN("invalid parameter: Ml->cbStruct = %d\n", Ml->cbStruct);
1361 return MMSYSERR_INVALPARAM;
1365 Ml->fdwLine = MIXERLINE_LINEF_DISCONNECTED;
1368 case MIXER_GETLINEINFOF_COMPONENTTYPE:
1370 Ml->dwLineID = 0xFFFF;
1371 TRACE("Looking for componenttype %d/%x\n", Ml->dwComponentType, Ml->dwComponentType);
1372 for (idx = 0; idx < mmixer->chans; ++idx)
1373 if (mmixer->lines[idx].component == Ml->dwComponentType)
1378 if (Ml->dwLineID == 0xFFFF)
1379 return MMSYSERR_KEYNOTFOUND;
1380 /* Now that we have lineid, fallback to lineid*/
1383 case MIXER_GETLINEINFOF_LINEID:
1384 if (Ml->dwLineID >= mmixer->chans)
1385 return MIXERR_INVALLINE;
1387 TRACE("MIXER_GETLINEINFOF_LINEID %d\n", Ml->dwLineID);
1388 Ml->dwDestination = mmixer->lines[Ml->dwLineID].dst;
1390 if (Ml->dwDestination != Ml->dwLineID)
1392 Ml->dwSource = getsrcfromline(mmixer, Ml->dwLineID);
1393 Ml->cConnections = 1;
1397 Ml->cConnections = getsrccntfromchan(mmixer, Ml->dwLineID);
1398 Ml->dwSource = 0xFFFFFFFF;
1400 TRACE("Connections %d, source %d\n", Ml->cConnections, Ml->dwSource);
1403 case MIXER_GETLINEINFOF_DESTINATION:
1404 if (Ml->dwDestination >= mmixer->dests)
1406 WARN("dest %d out of bounds\n", Ml->dwDestination);
1407 return MIXERR_INVALLINE;
1410 Ml->dwLineID = Ml->dwDestination;
1411 Ml->cConnections = getsrccntfromchan(mmixer, Ml->dwLineID);
1412 Ml->dwSource = 0xFFFFFFFF;
1415 case MIXER_GETLINEINFOF_SOURCE:
1416 if (Ml->dwDestination >= mmixer->dests)
1418 WARN("dest %d for source out of bounds\n", Ml->dwDestination);
1419 return MIXERR_INVALLINE;
1422 if (Ml->dwSource >= getsrccntfromchan(mmixer, Ml->dwDestination))
1424 WARN("src %d out of bounds\n", Ml->dwSource);
1425 return MIXERR_INVALLINE;
1428 Ml->dwLineID = getsrclinefromchan(mmixer, Ml->dwDestination, Ml->dwSource);
1429 Ml->cConnections = 1;
1432 case MIXER_GETLINEINFOF_TARGETTYPE:
1433 FIXME("TODO: TARGETTYPE, stub\n");
1434 return MMSYSERR_INVALPARAM;
1437 FIXME("Unknown query flag: %08lx\n", qf);
1438 return MMSYSERR_INVALPARAM;
1441 Ml->fdwLine &= ~MIXERLINE_LINEF_DISCONNECTED;
1442 Ml->fdwLine |= MIXERLINE_LINEF_ACTIVE;
1443 if (Ml->dwLineID >= mmixer->dests)
1444 Ml->fdwLine |= MIXERLINE_LINEF_SOURCE;
1446 mline = &mmixer->lines[Ml->dwLineID];
1447 Ml->dwComponentType = mline->component;
1448 Ml->cChannels = mmixer->lines[Ml->dwLineID].chans;
1451 for (i=CONTROLSPERLINE*Ml->dwLineID;i<CONTROLSPERLINE*(Ml->dwLineID+1); ++i)
1452 if (mmixer->controls[i].enabled)
1455 lstrcpynW(Ml->szShortName, mmixer->lines[Ml->dwLineID].name, sizeof(Ml->szShortName)/sizeof(WCHAR));
1456 lstrcpynW(Ml->szName, mmixer->lines[Ml->dwLineID].name, sizeof(Ml->szName)/sizeof(WCHAR));
1458 Ml->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
1460 Ml->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
1461 Ml->Target.dwDeviceID = 0xFFFFFFFF;
1462 Ml->Target.wMid = WINE_MIXER_MANUF_ID;
1463 Ml->Target.wPid = WINE_MIXER_PRODUCT_ID;
1464 Ml->Target.vDriverVersion = WINE_MIXER_VERSION;
1465 lstrcpynW(Ml->Target.szPname, mmixer->mixername, sizeof(Ml->Target.szPname)/sizeof(WCHAR));
1466 return MMSYSERR_NOERROR;
1469 /* Get the controls that belong to a certain line, either all or 1 */
1470 static DWORD MIX_GetLineControls(UINT wDevID, LPMIXERLINECONTROLSW mlc, DWORD_PTR flags)
1472 mixer *mmixer = MIX_GetMix(wDevID);
1476 if (!mlc || mlc->cbStruct != sizeof(*mlc))
1478 WARN("Invalid mlc %p, cbStruct: %d\n", mlc, (!mlc ? -1 : mlc->cbStruct));
1479 return MMSYSERR_INVALPARAM;
1482 if (mlc->cbmxctrl != sizeof(MIXERCONTROLW))
1484 WARN("cbmxctrl %d\n", mlc->cbmxctrl);
1485 return MMSYSERR_INVALPARAM;
1489 return MMSYSERR_BADDEVICEID;
1491 flags &= MIXER_GETLINECONTROLSF_QUERYMASK;
1493 if (flags == MIXER_GETLINECONTROLSF_ONEBYID)
1494 mlc->dwLineID = mlc->u.dwControlID / CONTROLSPERLINE;
1496 if (mlc->dwLineID >= mmixer->chans)
1498 TRACE("Invalid dwLineID %d\n", mlc->dwLineID);
1499 return MIXERR_INVALLINE;
1504 case MIXER_GETLINECONTROLSF_ALL:
1505 TRACE("line=%08x MIXER_GETLINECONTROLSF_ALL (%d)\n", mlc->dwLineID, mlc->cControls);
1506 for (i = 0; i < CONTROLSPERLINE; ++i)
1507 if (mmixer->controls[i+mlc->dwLineID * CONTROLSPERLINE].enabled)
1509 memcpy(&mlc->pamxctrl[j], &mmixer->controls[i+mlc->dwLineID * CONTROLSPERLINE].c, sizeof(MIXERCONTROLW));
1510 TRACE("Added %s (%s)\n", debugstr_w(mlc->pamxctrl[j].szShortName), debugstr_w(mlc->pamxctrl[j].szName));
1512 if (j > mlc->cControls)
1514 WARN("invalid parameter\n");
1515 return MMSYSERR_INVALPARAM;
1519 if (!j || mlc->cControls > j)
1521 WARN("invalid parameter\n");
1522 return MMSYSERR_INVALPARAM;
1525 case MIXER_GETLINECONTROLSF_ONEBYID:
1526 TRACE("line=%08x MIXER_GETLINECONTROLSF_ONEBYID (%x)\n", mlc->dwLineID, mlc->u.dwControlID);
1528 if (!mmixer->controls[mlc->u.dwControlID].enabled)
1529 return MIXERR_INVALCONTROL;
1531 mlc->pamxctrl[0] = mmixer->controls[mlc->u.dwControlID].c;
1533 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
1534 TRACE("line=%08x MIXER_GETLINECONTROLSF_ONEBYTYPE (%s)\n", mlc->dwLineID, getControlType(mlc->u.dwControlType));
1536 ct = mlc->u.dwControlType & MIXERCONTROL_CT_CLASS_MASK;
1537 for (i = 0; i <= CONTROLSPERLINE; ++i)
1539 const int ofs = i+mlc->dwLineID*CONTROLSPERLINE;
1540 if (i == CONTROLSPERLINE)
1542 WARN("invalid parameter: control %s not found\n", getControlType(mlc->u.dwControlType));
1543 return MIXERR_INVALCONTROL;
1545 if (mmixer->controls[ofs].enabled && (mmixer->controls[ofs].c.dwControlType & MIXERCONTROL_CT_CLASS_MASK) == ct)
1547 mlc->pamxctrl[0] = mmixer->controls[ofs].c;
1553 FIXME("Unknown flag %08lx\n", flags & MIXER_GETLINECONTROLSF_QUERYMASK);
1554 return MMSYSERR_INVALPARAM;
1557 return MMSYSERR_NOERROR;
1560 #endif /*HAVE_ALSA*/
1562 /**************************************************************************
1563 * mxdMessage (WINEALSA.3)
1565 DWORD WINAPI ALSA_mxdMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1566 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1570 TRACE("(%04X, %s, %08lX, %08lX, %08lX);\n", wDevID, getMessage(wMsg),
1571 dwUser, dwParam1, dwParam2);
1575 case DRVM_INIT: ALSA_MixerInit(); ret = MMSYSERR_NOERROR; break;
1576 case DRVM_EXIT: ALSA_MixerExit(); ret = MMSYSERR_NOERROR; break;
1577 /* All taken care of by driver initialisation */
1578 /* Unimplemented, and not needed */
1581 ret = MMSYSERR_NOERROR; break;
1584 ret = MIX_Open(wDevID, (LPMIXEROPENDESC) dwParam1, dwParam2); break;
1587 ret = MIX_Close(wDevID); break;
1589 case MXDM_GETDEVCAPS:
1590 ret = MIX_GetDevCaps(wDevID, (LPMIXERCAPS2W)dwParam1, dwParam2); break;
1592 case MXDM_GETLINEINFO:
1593 ret = MIX_GetLineInfo(wDevID, (LPMIXERLINEW)dwParam1, dwParam2); break;
1595 case MXDM_GETLINECONTROLS:
1596 ret = MIX_GetLineControls(wDevID, (LPMIXERLINECONTROLSW)dwParam1, dwParam2); break;
1598 case MXDM_GETCONTROLDETAILS:
1599 ret = MIX_GetControlDetails(wDevID, (LPMIXERCONTROLDETAILS)dwParam1, dwParam2); break;
1601 case MXDM_SETCONTROLDETAILS:
1602 ret = MIX_SetControlDetails(wDevID, (LPMIXERCONTROLDETAILS)dwParam1, dwParam2); break;
1604 case MXDM_GETNUMDEVS:
1608 WARN("unknown message %s!\n", getMessage(wMsg));
1609 return MMSYSERR_NOTSUPPORTED;
1612 TRACE("Returning %08X\n", ret);
1615 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1617 return MMSYSERR_NOTENABLED;
1618 #endif /*HAVE_ALSA*/